db.collection.estimatedDocumentCount()

On this page本页内容

Definition定义

db.collection.estimatedDocumentCount(options)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

New in version 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描述
options document Optional.可选。Extra options that affect the count behavior.影响计数行为的额外选项。

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

Field字段Type类型Description描述
maxTimeMS integer Optional.可选。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 the correct statistics after an unclean shutdown.mongod上的每个集合上运行validate,以在不干净的关闭后恢复正确的统计数据。

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()标记为终止(即,终止操作)。

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({})