Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify best practices for countDocuments() and estimatedDocumentCount() #6745

Open
vkarpov15 opened this issue Jul 19, 2018 · 6 comments
Open
Labels
discussion If you have any thoughts or comments on this issue, please share them!

Comments

@vkarpov15
Copy link
Collaborator

Re: discussion from #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.

@vkarpov15 vkarpov15 added the discussion If you have any thoughts or comments on this issue, please share them! label Jul 19, 2018
@ILoveYouDrZaius
Copy link

I agree with this issue, I don't understand why I have to switch between two functions.

@vkarpov15
Copy link
Collaborator Author

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.

@xxyyzz2050
Copy link

use count(0) instead of count({filter}) or countDocument({filter}) to run estimatedDocumentCount()

@libook
Copy link

libook commented Jun 28, 2019

Is it the best practice to use estimatedDocumentCount() when there is no query parameter?
If it is, it will be very helpful to switch between estimatedDocumentCount() and countDocuments() with a single method.

@vkarpov15
Copy link
Collaborator Author

@libook yes, you should use estimatedDocumentCount when there's no filter param. There may be some reasons to use countDocumenta() if you're using sharding, but I'm not certain.

@vkarpov15 vkarpov15 reopened this Jun 30, 2019
@shuwenhe
Copy link

CountDocuments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them!
Projects
None yet
Development

No branches or pull requests

5 participants