- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.estimatedDocumentCount()
db.collection.estimatedDocumentCount()¶
On this page
Definition¶
-
db.collection.estimatedDocumentCount(options)¶ New in version 4.0.3.
Returns the count of all documents in a collection or view. The method wraps the
countcommand.Parameter Type Description options document Optional. Extra options that affect the count behavior. The
optionsdocument can contain the following:Field Type Description maxTimeMSinteger Optional. The maximum amount of time to allow the count to run.
Behavior¶
db.collection.estimatedDocumentCount() does not take a query
filter and instead uses metadata to return the count for a collection.
On a sharded cluster, the resulting count will not correctly filter out orphaned documents.
After an unclean shutdown, the count may be incorrect.
After an unclean shutdown of a
mongodusing the Wired Tiger storage engine, count statistics reported bydb.collection.estimatedDocumentCount()may be inaccurate.The amount of drift depends on the number of insert, update, or delete operations performed between the last checkpoint and the unclean shutdown. Checkpoints usually occur every 60 seconds. However,
mongodinstances running with non-default--syncdelaysettings may have more or less frequent checkpoints.Run
validateon each collection on themongodto restore the correct statistics after an unclean shutdown.
Example¶
The following example uses
db.collection.estimatedDocumentCount to retrieve the count of
all documents in the orders collection:
See also