I would like to know how to count the number of documents in a collection. I tried the follow
var value = collection.count();
&&
var value = collection.find().count()
&&
var value = collection.find().dataSize()
I always get method undefined.
Can you let me know what is the method to use to find the total documents in a collection.
Thanks Ganesh
Since v4.0.3 you can use for better performance:
db.collection('collection-name').count()
is now deprecated. Instead of count(), we should now usecountDocuments()
andestimatedDocumentCount()
. Here is an example:To know more read the documentation:
https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/
https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/