Unify best practices for countDocuments() and estimatedDocumentCount()
See original GitHub issueRe: discussion from https://github.com/Automattic/mongoose/issues/6713, leaving a note to think about whether we should add a method that switches between countDocuments()
and estimatedDocumentCount()
based on whether filter
is empty or not.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:6
Top Results From Across the Web
db.collection.countDocuments() — MongoDB Manual
Count all Documents that Match a Query¶ · db.collection.estimatedDocumentCount() · $group and $sum · count · collStats pipeline stage with the count option....
Read more >MongoDB count() versus countDocuments() - Stack Overflow
The db.collection.find method returns a cursor. The cursor.count() method on the cursor counts the number of documents referenced by a ...
Read more >Mongoose v6.8.2: API docs
Mongoose.prototype.connect(). Parameters. uri «String» mongodb URI to connect to. [options] «Object» passed down ...
Read more >Collection - Node.js MongoDB Driver API
new Collection(){Collection} ... Deprecated. use countDocuments or estimatedDocumentCount instead ... countDocuments(query, options, callback){Promise}.
Read more >Collection level operations — PyMongo 4.3.3 documentation
coll1.read_preference Primary() >>> from pymongo import ReadPreference ... when $out or $merge are used on MongoDB <5.0, in which case PRIMARY is used....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There’s some more detail about why you would use one vs the other in the API docs:
TLDR;
estimatedDocumentCount()
is faster when you’re looking to count how many documents are in the entire collection, but might not be 100% accurate in a sharded cluster. If you’re using a standalone MongoDB instance and have less than 100k documents per collection, it doesn’t make much difference whether you useestimatedDocumentCount()
orcountDocuments()
. It can make a big difference with large collections though.CountDocuments