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})
> 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
3
> db.people.find({"age" : 38}).count()
1
1
> db.people.find({"age" : {"$gt" : 30}}).count()
2
2
Getting Number of Documets in Collection