Database Manual / Reference / mongosh Methods / Collections

db.collection.estimatedDocumentCount() (mongosh method方法)

Definition定义

db.collection.estimatedDocumentCount(options)

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

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

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

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

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

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. However, mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.检查点通常每60秒出现一次。然而,使用非默认的--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客户端断开连接

If the client that issued db.collection.estimatedDocumentCount() disconnects before the operation completes, MongoDB marks db.collection.estimatedDocumentCount() for termination using killOp.如果发出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({})