On this page本页内容
cursor.
count
()¶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 that corresponds to countDocuments()
and estimatedDocumentCount()
. count()
API,而支持对应于countDocuments()
和estimatedDocumentCount()
的新API。For the specific API names for a given driver, see the driver API documentation.有关给定驱动程序的特定API名称,请参阅驱动程序API文档。
Counts the number of documents referenced by a cursor. 统计游标引用的文档数。Append the 将count()
method to a find()
query to return the number of matching documents. count()
方法附加到find()
查询以返回匹配文档的数量。The operation does not perform the query but instead counts the results that would be returned by the query.该操作不执行查询,而是统计查询将返回的结果。
Important重要的
count()
if the find()
operation is run without a query predicate since without the query predicate, these count()
returns results based on the collection’s metadata, which may result in an approximate count. find()
操作,请避免使用count()
,因为如果没有查询谓词,这些count()
将基于集合的元数据返回结果,这可能会导致近似计数。The count()
method has the following prototype form:count()
方法的原型形式如下:
The count()
method has the following parameter:count()
方法具有以下参数:
applySkipLimit |
boolean | cursor.skip() and cursor.limit() methods in the count. cursor.skip() 和cursor.limit() 方法的影响。count() method ignores the effects of the cursor.skip() and cursor.limit() . count() 方法忽略cursor.skip() 和cursor.limit() 的效果。applySkipLimit to true to consider the effect of these methods.applySkipLimit 设置为true ,以考虑这些方法的效果。 |
MongoDB also provides an equivalent MongoDB还提供了一个等效的db.collection.count()
as an alternative to the db.collection.find(<query>).count()
construct.db.collection.count()
,作为db.collection.find(<query>).count()
构造的替代。
MongoDB supports the use of MongoDB支持在hint()
with count()
. count()
中使用hint()
。See Specify the Index to Use for an example.有关示例,请参阅指定要使用的索引。
See also参阅
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, 在分片集群上,如果存在孤立文档或正在进行区块迁移,则count()
without a query predicate in the find
can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.find
中没有查询谓词的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将文档分页到内存中,以便相同计数操作的后续调用具有更好的性能。
The following are examples of the 下面是count()
method.count()
方法的示例。
The following operation counts the number of all documents in the 以下操作统计orders
collection:orders
集合中所有文档的数量:
The following operation counts 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 following operation counts the number of the documents in the 以下操作计算orders
collection with the field ord_dt
greater than new Date('01/01/2012')
taking into account the effect of the limit(5)
:orders
集合中ord_dt
字段大于new Date('01/01/2012')
的文档数量,并考虑limit(5)
的影响:
The following operation uses the index named 以下操作使用名为"status_1"
, which has the index key specification of { status: 1 }
, to return a count of the documents in the orders
collection with the field ord_dt
greater than new Date('01/01/2012')
and the status
field is equal to "D"
:"status_1"
的索引,其索引键规范为{status:1}
,返回orders
集合中的文档计数,字段ord_dt
大于new Date('01/01/2012')
,status
字段等于"D"
: