Docs HomeMongoDB Manual

db.collection.estimatedDocumentCount()

On this page本页内容

Definition定义

db.collection.estimatedDocumentCount(options)
Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the count field returned by the collStats command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

Returns the count of all documents in a collection or view.返回集合或视图中所有文档的计数。

db.collection.estimatedDocumentCount( <options> )
Parameter参数Type类型Description描述
optionsdocumentOptional.可选的。Extra options that affect the count behavior.影响计数行为的额外选项。

The options document can contain the following:options文档可以包含以下内容:

Field字段Type类型Description描述
maxTimeMSintegerOptional.可选的。The maximum amount of time to allow the count to run.允许运行计数的最长时间。

Behavior行为

Mechanics机械师

db.collection.estimatedDocumentCount() does not take a query filter and instead uses metadata to return the count for a collection.不采用查询筛选器,而是使用元数据返回集合的计数。

For a view:对于视图

  • There is no metadata.没有元数据。
  • The document count is calculated by executing the aggregation pipeline in the view definition.文档计数是通过执行视图定义中的聚合管道来计算的。
  • There is no fast estimated document count.没有快速估计的文档计数。

Sharded Clusters分片集群

On a sharded cluster, the resulting count will not correctly filter out orphaned documents.在分片集群上,生成的计数将无法正确筛选出孤立文档

Unclean Shutdown不清洁关闭

This section only applies to collections.本节仅适用于集合。

After an unclean shutdown, the count may be incorrect.不干净关机后,计数可能不正确。

After an unclean shutdown of a mongod using the Wired Tiger storage engine, count statistics reported by db.collection.estimatedDocumentCount() may be inaccurate.在使用Wired Tiger存储引擎不干净地关闭mongod之后,db.collection.estimatedDocumentCount()报告的计数统计数据可能不准确。

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. 检查点通常每60秒出现一次。However, mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.然而,使用非默认--syncdelay设置运行的mongod实例可能或多或少有频繁的检查点。

Run validate on each collection on the mongod to restore statistics after an unclean shutdown.mongod上的每个集合运行validate,以在不干净的关闭后恢复统计信息。

After an unclean shutdown:不干净关机后:

Client Disconnection客户端断开连接

Starting in MongoDB 4.2, if the client that issued db.collection.estimatedDocumentCount() disconnects before the operation completes, MongoDB marks db.collection.estimatedDocumentCount() for termination using killOp.从MongoDB 4.2开始,如果发出db.collection.estimatedDocumentCount()的客户端在操作完成前断开连接,MongoDB会使用killOp标记db.collection.estimatedDocumentCount()终止。

Count and Transactions计数和事务

When you use db.collection.estimatedDocumentCount() in a transaction, the resulting count will not filter out any uncommitted multi-document transactions.在事务中使用db.collection.estimatedDocumentCount()时,生成的计数将不会筛选掉任何未提交的多文档事务

Example实例

The following example uses db.collection.estimatedDocumentCount() to retrieve the count of all documents in the orders collection:以下示例使用db.collection.estimatedDocumentCount()检索orders集合中所有文档的计数:

db.orders.estimatedDocumentCount({})