We use estimatedDocumentCount to quickly find out how many documents are in a collection without scanning all data.
0
0
estimatedDocumentCount for speed in MongoDB
Introduction
When you want a fast count of all documents in a large collection.
When exact count is not critical but speed is important.
When you need to show an approximate number of items to users quickly.
When running analytics that can tolerate small differences in count.
When you want to avoid slowing down your database with heavy count queries.
Syntax
MongoDB
db.collection.estimatedDocumentCount()
This method returns an approximate count of documents in the collection.
It does not take any filter or condition; it counts all documents.
Examples
Get an approximate count of all documents in the
users collection.MongoDB
db.users.estimatedDocumentCount()
Quickly find how many orders are in the
orders collection.MongoDB
db.orders.estimatedDocumentCount()
Sample Program
This example switches to the shopDB database and gets an estimated count of documents in the products collection. It then prints the count.
MongoDB
use shopDB // Get estimated count of products const count = db.products.estimatedDocumentCount(); print('Estimated product count:', count);
OutputSuccess
Important Notes
The count is fast because it uses collection metadata, not scanning documents.
For exact counts with filters, use countDocuments() instead.
Estimated counts may be slightly off if the collection is being updated.
Summary
estimatedDocumentCount() gives a fast, approximate total count of documents.
It is useful when speed matters more than exact numbers.
It does not accept filters or conditions.