Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How accurate is MongoDB's estimated count query?

Tags:

mongodb

The official MongoDB driver offers a 'count' and 'estimated document count' API, as far as I know the former command is highly memory intensive so it's recommended to use the latter in situations that require it.

But how accurate is this estimated document count? Can the count be trusted in a Production environment, or is using the count API recommended when absolute accuracy is needed?

like image 704
ChazMcDingle Avatar asked Nov 29 '18 15:11

ChazMcDingle


People also ask

How do you count the no of documents retrieved from MongoDB?

count() The count() method counts the number of documents that match the selection criteria. It returns the number of documents that match the selection criteria.

How do I count documents in a collection?

count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.

How do I count documents in MongoDB using Python?

Python3. Method 2: count_documents() Alternatively, you can also use count_documents() function in pymongo to count the number of documents present in the collection. Example: Retrieves the documents present in the collection and the count of the documents using count_documents().

What is estimatedDocumentCount in mongoose?

In MongoDB the db. collection. estimatedDocumentCount() method returns the count of all documents in a collection or view. The collection part is the name of the collection or view to perform the count operation on.


1 Answers

Comparing the two, to me it's very difficult to conjure up a scenario in which you'd want to use countDocuments() when estimatedDocumentCount() was an option.

That is, the equivalent form of estimatedDocumentCount() is countDocuments({}), i.e., an empty query filter. The cost of the first function is O(1); the second is O(N), and if N is very large, the cost will be prohibitive.

Both return a count, which, in a scenario in which Mongo has been deployed, is likely to be quite ephemeral, i.e., it's inaccurate the moment you have it, as the collection changes.

like image 57
Allan Bazinet Avatar answered Oct 26 '22 09:10

Allan Bazinet