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?
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.
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.
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().
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With