On this page本页内容
db.collection.countDocuments(query, options)
This is a 这是蒙古方法。mongosh
method. This is not the documentation for 这不是Node.js或其他特定于编程语言的驱动程序方法的文档。Node.js
or other programming language specific driver methods.
In most cases, 在大多数情况下,mongosh
methods work the same way as the legacy mongo
shell methods. mongosh
方法的工作方式与传统mongo
shell方法相同。However, some legacy methods are unavailable in 然而,一些遗留方法在mongosh
.mongosh
中不可用。
For the legacy 有关旧版mongo shell文档,请参阅相应MongoDB Server版本的文档: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 documents that match the query for a collection or view. 返回与集合或视图的查询匹配的文档数。The method wraps the 该方法使用$group
aggregation stage with a $sum
expression to perform the count and is available for use in Transactions.$sum
表达式包装$group
聚合阶段以执行计数,可在事务中使用。
db.collection.countDocuments( <query>, <options> )
query | document | |
options | document |
The options
document can contain the following:options
文档可以包含以下内容:
limit | integer | |
skip | integer | |
hint | string or document | |
maxTimeMS | integer |
Unlike 与db.collection.count()
, db.collection.countDocuments()
does not use the metadata to return the count. db.collection.count()
不同,db.collection.countDocuments()
不使用元数据返回计数。Instead, it performs an aggregation of the document to return an accurate count, even after an unclean shutdown or in the presence of orphaned documents in a sharded cluster.相反,它会对文档执行聚合以返回准确的计数,即使在不干净的关闭之后或在分片集群中存在孤立文档的情况下也是如此。
db.collection.countDocuments()
wraps the following aggregation operation and returns just the value of 包装以下聚合操作并仅返回n
:n
的值:
db.collection.aggregate([ { $match: <query> }, { $group: { _id: null, n: { $sum: 1 } } } ])
Starting in version 4.2.1 (and 4.0-series in 4.0.13), 从版本4.2.1(和4.0.13中的4.0-series)开始,db.collection.countDocuments()
returns 0
on an empty or non-existing collection or view.db.collection.countDocuments()
对空的或不存在的集合或视图返回0
。
In earlier versions of MongoDB, 在早期版本的MongoDB中,空的或不存在的集合或视图上出现db.collection.countDocuments()
errors on an empty or non-existing collection or view.db.collection.countDocuments()
错误。
You cannot use the following query operators as part of the query expression for 不能将以下查询运算符用作db.collection.countDocuments()
:db.collection.countDocuments()
的查询表达式的一部分:
$where | $expr instead.$expr 。 |
$near | As an alternative, use { $geoWithin: { $center: [ [ <x>, <y> ], <radius> ] } } |
$nearSphere | As an alternative, use { $geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] } } |
db.collection.countDocuments()
can be used inside multi-document transactions.可以在多文档事务中使用。
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会带来更大的性能成本,并且多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. 对于许多场景,非规范化的数据模型(嵌入式文档和数组)将继续是您的数据和用例的最佳选择。That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,适当地建模数据将最小化多文档事务的需要。
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.collection.countDocuments()
disconnects before the operation completes, MongoDB marks the db.collection.countDocuments()
for termination (i.e. killOp
on the operation).db.collection.countDocuments()
的客户端在操作完成之前断开连接,MongoDB会将db.collection.countDocuments()
标记为终止(即操作上的killOp
)。
To count the number of all documents in the 要计算orders
collection, use the following operation:orders
集合中所有文档的数量,请使用以下操作:
db.orders.countDocuments({})
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')
的文档数:
db.orders.countDocuments( { ord_dt: { $gt: new Date('01/01/2012') } }, { limit: 100 } )