Wonder how to get the number of ducoments in MongoDB collection? The following example use count() method to a find() query to return the number of matching documents.

Assume that we have the following documents in a collection.

> db.people.insert({"name" : "John", "age" : 38})
> db.people.insert({"name" : "Kelly", "age" : 26})
> db.people.insert({"name" : "Sarah", "age" : 36})

To get the total count of documents in the collection:

> db.people.find().count()
3
To get the total count of documents which age equal to 38 in the collection:
> db.people.find({"age" : 38}).count()
1
To get the total count of documents which age greater than 30 in the collection:
> db.people.find({"age" : {"$gt" : 30}}).count()
2