estimatedDocumentCount() (Estimate Document Count)
Last updated: 2026-10-09
estimatedDocumentCount() (Estimate Document Count)
estimatedDocumentCount() estimates the total document count from collection metadata, e.g. `db.users.estimatedDocumentCount()`. It takes no filter and scans nothing, so it is extremely fast, but the value can lag reality (especially on unflushed sharded clusters). It suits dashboards and capacity estimates where precision is not critical; for exact counts use countDocuments().
| Category | Read |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.estimatedDocumentCount()
Returns:Estimated total document count (integer).
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// estimatedDocumentCount: quickly check total users
const n = db.users.estimatedDocumentCount()
print('Total users (estimate): ' + n)
Output:
Total users (estimate): 1000
📌 Notes
- Takes no filter — whole-collection only
- Metadata-based and may lag behind reality
- Fast but not guaranteed precise; use countDocuments() when exactness matters
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhy is estimatedDocumentCount faster than countDocuments?
AIt reads the count from collection metadata without scanning documents; countDocuments() actually evaluates the filter.
QIs estimatedDocumentCount accurate?
AUsually close, but not guaranteed; heavy writes or sharding can widen the gap.
QWhich count method should I pick?
AUse countDocuments() for exact or filtered counts; estimatedDocumentCount() when magnitude and speed matter.