question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unify best practices for countDocuments() and estimatedDocumentCount()

See original GitHub issue

Re: 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:open
  • Created 5 years ago
  • Reactions:8
  • Comments:6

github_iconTop GitHub Comments

7reactions
vkarpov15commented, Sep 30, 2018

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 use estimatedDocumentCount() or countDocuments(). It can make a big difference with large collections though.

0reactions
shuwenhecommented, Aug 19, 2019

CountDocuments

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found