On this page本页内容
db.collection.estimatedDocumentCount(options)
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> )
options | document |
The options
document can contain the following:options
文档可以包含以下内容:
maxTimeMS | integer |
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.不干净停机后,计数可能不正确。
After an unclean shutdown of a 在使用Wired Tiger存储引擎不干净地关闭mongod
using the Wired Tiger storage engine, count statistics reported by db.collection.estimatedDocumentCount()
may be inaccurate.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:不干净关机后:
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.collection.estimatedDocumentCount()
disconnects before the operation completes, MongoDB marks the db.collection.estimatedDocumentCount()
for termination (i.e. killOp
on the operation).db.collection.estimatedDocumentCount()
的客户端在操作完成之前断开连接,MongoDB会将db.collection.estimatedDocumentCount()
标记为终止(即操作上的killOp
)。
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({})