db.collection.estimatedDocumentCount()

On this page本页内容

Definition定义

db.collection.estimatedDocumentCount(options)
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell 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.在版本4.0.3中新增

Returns the count of all documents in a collection or view. 返回集合或视图中所有文档的计数。The method wraps the count command.该方法包装了count命令。

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.不采用查询筛选器,而是使用元数据返回集合的计数。

Sharded Clusters分片群集

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

Unclean Shutdown不干净关机

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 the db.collection.estimatedDocumentCount() disconnects before the operation completes, MongoDB marks the db.collection.estimatedDocumentCount() for termination (i.e. killOp on the operation).从MongoDB 4.2开始,如果发出db.collection.estimatedDocumentCount()的客户端在操作完成之前断开连接,MongoDB会将db.collection.estimatedDocumentCount()标记为终止(即操作上的killOp)。

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({})
←  db.collection.ensureIndex()db.collection.explain() →