- Importantmongosh Method
This is a
mongoshmethod. This is not the documentation for
Node.jsor other programming language specific driver methods.
For the legacy
mongoshell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
New in version 4.0.3.
Returns the count of all documents in a collection or view. The method wraps the
db.collection.estimatedDocumentCount( <options> )ParameterTypeDescriptionoptionsdocumentOptional. Extra options that affect the count behavior.
optionsdocument can contain the following:FieldTypeDescription
maxTimeMSintegerOptional. The maximum amount of time to allow the count to run.
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.
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,
mongod instances running
--syncdelay settings may have more or less frequent
Starting in MongoDB 4.2, if the client that issued the
disconnects before the operation completes, MongoDB marks
db.collection.estimatedDocumentCount() for termination (i.e.
killOp on the
The following example uses
db.collection.estimatedDocumentCount() to retrieve the count of
all documents in the