- Reference >
- mongo Shell Methods >
- Cursor Methods >
- cursor.count()
cursor.count()¶
说明¶
- cursor.count()¶
返回游标引用的文档记录数。在 find() 方法后追加 count() 方法来查询匹配的文档记录数。这个操作并没有执行完整的查询,它只取得本次查询可以返回的结果的数量。
在 2.6 版更改: MongoDB 支持 hint() 方法和 count() 方法联合使用。参见 Specify the Index to Use 中的例子。
The count() method has the following prototype form:
db.collection.find(<query>).count()
The count() method has the following parameter:
Parameter Type Description applySkipLimit boolean Optional. Specifies whether to consider the effects of the cursor.skip() and cursor.limit() methods in the count. By default, the count() method ignores the effects of the cursor.skip() and cursor.limit(). Set applySkipLimit to true to consider the effect of these methods. MongoDB 提供了与 db.collection.count() 方法效果相同的 db.collection.find(<query>).count() 方法,可以任选一个使用。
行为¶
分片集群¶
On a sharded cluster, count() can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.
为了避免这些情况,在分片集群中,可以使用聚合函数( db.collection.aggregate() )中的 $group 操作符对集合执行求和( $sum )。例如,下面的操作计算集合中的文档数量:
db.collection.aggregate(
[
{ $group: { _id: null, count: { $sum: 1 } } }
]
)
如果想要进行带查询条件的计数,使用 $match 操作符是个好方法:
db.collection.aggregate(
[
{ $match: <query condition> },
{ $group: { _id: null, count: { $sum: 1 } } }
]
)
参见 Perform a Count 中的例子。
使用索引¶
参考包含如下索引的集合:
{ a: 1, b: 1 }
正在执行计数时,如果满足以下条件,MongoDB 会返回只用索引计算出来的数量:
查询可以使用索引
查询条件中只包含创建过索引的键,并且
查询只需要使用索引中一块连续的区域:
For example, the following operations can return the count using only the index:
db.collection.find( { a: 5, b: 5 } ).count()
db.collection.find( { a: { $gt: 5 } } ).count()
db.collection.find( { a: 5, b: { $gt: 10 } } ).count()
无论何时,如果查询可以使用索引但不是只访问索引中一块连续的区域,或者查询条件中包含索引以外的字段,这时就不能使用索引,MongoDB 只能读取文档来返回计算的数量。
db.collection.find( { a: 5, b: { $in: [ 1, 2, 3 ] } } ).count()
db.collection.find( { a: { $gt: 5 }, b: 5 } ).count()
db.collection.find( { a: 5, b: 5, c: 5 } ).count()
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.
Examples¶
The following are examples of the count() method.
Count All Documents¶
The following operation counts the number of all documents in the orders collection:
db.orders.find().count()
Count Documents That Match a Query¶
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'):
db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).count()
Limit Documents in Count¶
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):
db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).limit(5).count(true)
Specify the Index to Use¶
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":
db.orders.find(
{ ord_dt: { $gt: new Date('01/01/2012') }, status: "D" }
).hint( "status_1" ).count()