On this page本页内容
db.collection.
count
(query, 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驱动程序文档。
Note
MongoDB drivers compatible with the 4.0 features deprecate their respective cursor and collection 与4.0功能兼容的MongoDB驱动程序不支持各自的游标和集合count()
APIs in favor of new APIs for countDocuments()
and estimatedDocumentCount()
. count()
API,而支持countDocuments()
和estimatedDocumentCount()
的新API。For the specific API names for a given driver, see the driver documentation.有关给定驱动程序的特定API名称,请参阅驱动程序文档。
Returns the count of documents that would match a 返回与集合或视图的find()
query for the collection or view. find()
查询匹配的文档数。The db.collection.count()
method does not perform the find()
operation but instead counts and returns the number of results that match a query.db.collection.count()
方法不执行find()
操作,而是计算并返回与查询匹配的结果数。
Important
db.collection.count()
method without a query predicate since without the query predicate, the method returns results based on the collection’s metadata, which may result in an approximate count. db.collection.count()
方法,因为如果没有查询谓词,该方法将基于集合的元数据返回结果,这可能会导致近似计数。In particular,query |
document | |
options |
document |
The options
document contains the following fields:options
文档包含以下字段:
limit |
integer | |
skip |
integer | |
hint |
string or document | |
maxTimeMS |
integer | |
readConcern |
string |
|
collation |
document |
|
count()
is equivalent to the 相当于db.collection.find(query).count()
construct.db.collection.find(query).count()
构造。
You cannot use 不能在事务中使用count
and shell helpers count()
and db.collection.count()
in transactions.count
和shell助手count()
和db.collection.count()
。
For details, see Transactions and Count Operations.有关详细信息,请参阅事务和计数操作。
On a sharded cluster, 在分片集群上,如果存在孤立文档或正在进行区块迁移,则没有查询谓词的db.collection.count()
without a query predicate can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.db.collection.count()
可能会导致计数不准确。
To avoid these situations, on a sharded cluster, use the 要避免这些情况,请在分片集群上使用db.collection.aggregate()
method:db.collection.aggregate()
方法:
You can use the 您可以使用$count
stage to count the documents. $count
阶段对文档进行计数。For example, the following operation counts the documents in a collection:例如,以下操作对集合中的文档进行计数:
The $count
stage is equivalent to the following $group
+ $project
sequence:$count
阶段相当于以下$group
+$project
序列:
See also参阅
$collStats
to return an approximate count based on the collection’s metadata.返回基于集合元数据的近似计数。
Consider a collection with the following index:考虑一个具有以下索引的集合:
When performing a count, MongoDB can return the count using only the index if:在执行计数时,MongoDB可以仅使用索引返回计数,前提是:
For example, the following operations can return the count using only the index:例如,以下操作可以仅使用索引返回计数:
If, however, the query can use an index but the query predicates do not access a single contiguous range of index keys or the query also contains conditions on fields outside the index, then in addition to using the index, MongoDB must also read the documents to return the count.但是,如果查询可以使用索引,但查询谓词不访问单个连续范围的索引键,或者查询还包含索引外字段的条件,那么除了使用索引外,MongoDB还必须读取文档以返回计数。
In such cases, during the initial read of the documents, MongoDB pages the documents into memory such that subsequent calls of the same count operation will have better performance.在这种情况下,在最初读取文档的过程中,MongoDB将文档分页到内存中,以便相同计数操作的后续调用具有更好的性能。
After an unclean shutdown of a 在使用Wired Tiger存储引擎不干净地关闭mongod
using the Wired Tiger storage engine, count statistics reported by count()
may be inaccurate.mongod
后,count()
报告的计数统计数据可能不准确。
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
,以在不干净的关闭后恢复正确的统计数据。
Note
This loss of accuracy only applies to 这种精度损失仅适用于不包含查询谓词的count()
operations that do not include a query predicate.count()
操作。
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.collection.count()
disconnects before the operation completes, MongoDB marks the db.collection.count()
for termination (i.e. killOp
on the operation).db.collection.count()
的客户端在操作完成之前断开连接,MongoDB会将db.collection.count()
标记为终止(即终止操作)。
To count the number of all documents in the 要计算orders
collection, use the following operation:orders
集合中所有文档的数量,请使用以下操作:
This operation is equivalent to the following:此操作相当于以下操作:
Count the number of the documents in the 计算orders
collection with the field ord_dt
greater than new Date('01/01/2012')
:orders
集合中ord_dt
字段大于new Date('01/01/2012')
的文档数:
The query is equivalent to the following:该查询相当于以下内容: