Basically title. I’m using Node and for pagination I’m returning 10 documents at a time, but counting the full collection so that my table can paginate correctly. Do the counts incur a read for each document counted? And if so, would using estimatedDocumentCount() not, as it just reads the collections metadata?
Hello @Travis_Aucoin, welcome to the MongoDB Community forum!
MongoDB NodeJS Driver API has three Collection
class methods related with counting documents:
countDocuments: Gets the number of documents matching the filter. This gets the accurate count.
For a fast count of the total documents in a collection see estimatedDocumentCount: Gets an estimate of the count of documents in a collection using collection metadata.
count: An estimated count of matching documents in the db to a filter. NOTE: This method has been deprecated, since it does not provide an accurate count of the documents in a collection. To obtain an accurate count of documents in the collection, use countDocuments
. To obtain an estimated count of all documents in the collection, use estimatedDocumentCount
.
There is also a FindCursor#count: Get the count of documents for this cursor. This method is deprecated. Use collection.estimatedDocumentCount
or collection.countDocuments
instead.
Related References: