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 = collecti
db.collection('collection-name').count()
is now deprecated.
Instead of count(), we should now use countDocuments()
and estimatedDocumentCount()
.
Here is an example:
`db.collection('collection-name').countDocuments().then((docs) =>{
console.log('Number of documents are', docs);
};`
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/