How to Use MongoDB db.collection.count() With Examples

In this step by step MongoDB tutorial we will tell you how to use db.collection.count() function with examples. It’s the method used for collections or view that returns the documents count for Find() query.  Technically we use collection to perform the operation to view or count which represents the name of the collection that we are performing an action on.

One thing should be noted that in this tutorial we are not using the Find() operation but simply counting. So the return results will be the count matching the query.

Here is the example in which “Pets” represents the collection that has the following documents mentioned in it.

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
{ "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 }
{ "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }

Now we will use this db.collection.count() and replace collection with Pets as mentioned below to address this collection




So if we would add find() query in this method it would still return the same results. See the example below:




If you read MongoDB documentation you will see that it’s advised to not use db.collection.count() without a query predicate. It’s mainly due to when we use it, we get results based on the collection’s metadata which might be an approximate count.

Count the Result of a Query
We can count the result of a query by passing the query criteria. See this example for instance:

"type": "Dog"



In this example, we found out that there are four dogs in the collection. So the result shows us that there are total 4 dogs in the Pets collection.

Here is one more example to check the count of one more query for more clarity. In this we will find the number of pets with greater weight than a certain number.

"weight": { $gt: 10 }



Now we know that in this collection there are 3 pets with a greater weight.

Limit the Count
Now we are going to restricting the count query by using limit parameter to specify a certain number. See the example below:

db.pets.count( {}, { limit: 5 } )



In the very first example we found out that there are 7 documents in total in the collection “Pets”. In the above example we limited it to 5. On the other hand, if the limit exceeds the actual document count then the limit argument won’t affect it.

See this example for instance:

db.pets.count( {}, { limit: 10 } )



Skipping Documents 
So let’s say if we have to skip a number of documents in the collection while counting that we have shown in the example below:

db.pets.count( {}, { skip: 5 } )



MongoDB Deprecation
We need to keep this in mind that MongoDB suggests the following

MongoDB drivers compatible with the 4.0 features deprecate their respective cursor and collection count() APIs in favor of new APIs for countDocuments() and estimatedDocumentCount(). For the specific API names for a given driver, see the driver documentation here. 

More Information
One should be noted that db.collection.count() method is a wrapper for the count command and also accepts several parameters including maxTimeMS, readConcern, collation and hint.

In this tutorial we have tried to cover all the potential uses of db.collection.count() and when to add certain parameters along with to make an effective use of this method. Let’s know in the comments for further queries.