On this page本页内容
count
Counts the number of documents in a collection or a view. 统计集合或视图中的文档数。Returns a document that contains this count and as well as the command status.返回包含此计数和命令状态的文档。
MongoDB drivers compatible with the 4.0 features deprecate their respective cursor and collection 与4.0功能兼容的MongoDB驱动程序不支持各自的游标和集合count()
APIs (which runs the count
command) in favor of new APIs that corresponds to countDocuments()
and estimatedDocumentCount()
. count()
API(运行count
命令),而支持与countDocuments()
和estimatedDocumentCount()
相对应的新API。For the specific API names for a given driver, see the driver API documentation.有关给定驱动程序的特定API名称,请参阅驱动程序API文档。
count
has the following form:具有以下形式:
{
count: <collection or view>,
query: <document>,
limit: <integer>,
skip: <integer>,
hint: <hint>,
readConcern: <document>,
collation: <document>,
comment: <any>
}
count
has the following fields:
count | string | |
query | document | |
limit | integer | |
skip | integer | |
hint | string or document | |
readConcern | document |
readConcern: { level: <value> }
|
collation | document |
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }
|
comment | any |
|
mongosh
also provides the following wrapper methods for 还提供了以下count
:count
包装方法:
Avoid using the 避免在没有查询谓词的情况下使用count
and its wrapper methods without a query predicate (note: db.collection.estimatedDocumentCount()
does not take a query predicate) since without the query predicate, these operations return results based on the collection's metadata, which may result in an approximate count. count
及其包装方法(注意:db.collection.estimatedDocumentCount()
不接受查询谓词),因为如果没有查询谓语,这些操作将根据集合的元数据返回结果,这可能导致近似计数。In particular,特别地,
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, the 在分片集群上,如果存在孤立文档或正在进行块迁移,则在没有查询谓词的情况下运行count
command when run without a query predicate can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.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:例如,以下操作对集合中的文档进行计数:
db.collection.aggregate( [ { $count: "myCount" } ])
The $count
stage is equivalent to the following $group
+ $project
sequence:$count
阶段相当于以下$group
+$project
序列:
db.collection.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } } { $project: { _id: 0 } } ] )
$collStats
to return an approximate count based on the collection's metadata.以基于集合的元数据返回近似计数。
After an unclean shutdown of a 在使用Wired Tiger存储引擎不干净地关闭了mongod
using the Wired Tiger storage engine, count statistics reported by count
may be inaccurate.mongod
之后,计数报告的计数统计数据可能不准确。
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开始,如果发出count
disconnects before the operation completes, MongoDB marks the count
for termination (i.e. killOp
on the operation).count
的客户端在操作完成之前断开连接,MongoDB将count
标记为终止(即操作上的killOp
)。
The following sections provide examples of the 以下部分提供了count
command.count
命令的示例。
The following operation counts the number of all documents in the 以下操作统计orders
collection:orders
集合中所有文档的数量:
db.runCommand( { count: 'orders' } )
In the result, the 结果,表示计数的n
, which represents the count, is 26
, and the command status ok
is 1
:n
为26
,命令状态ok
为1
:
{ "n" : 26, "ok" : 1 }
The following operation returns a count of the documents in the 以下操作返回orders
collection where the value of the ord_dt
field is greater than Date('01/01/2012')
:orders
集合中ord_dt
字段值大于Date('01/01/2012')
的文档计数:
db.runCommand( { count:'orders', query: { ord_dt: { $gt: new Date('01/01/2012') } } } )
In the result, the 结果,表示计数的n
, which represents the count, is 13
and the command status ok
is 1
:n
为13
,命令状态ok
为1
:
{ "n" : 13, "ok" : 1 }
The following operation returns a count of the documents in the 以下操作返回orders
collection where the value of the ord_dt
field is greater than Date('01/01/2012')
and skip the first 10
matching documents:orders
集合中ord_dt
字段值大于Date('01/01/2012')
的文档计数,并跳过前10
个匹配的文档:
db.runCommand( { count:'orders', query: { ord_dt: { $gt: new Date('01/01/2012') } }, skip: 10 } )
In the result, the 结果,表示计数的n
, which represents the count, is 3
and the command status ok
is 1
:n
为3
,命令状态ok
为1
:
{ "n" : 3, "ok" : 1 }
The following operation uses the index 以下操作使用索引{ status: 1 }
to return a count of the documents in the orders
collection where the value of the ord_dt
field is greater than Date('01/01/2012')
and the status
field is equal to "D"
:{ status: 1 }
返回orders
集合中的文档计数,其中ord_dt
字段的值大于Date('01/01/2012')
且status
字段等于"D"
:
db.runCommand( { count:'orders', query: { ord_dt: { $gt: new Date('01/01/2012') }, status: "D" }, hint: { status: 1 } } )
In the result, the 结果,表示计数的n
, which represents the count, is 1
and the command status ok
is 1
:n
为1
,命令状态ok
为1
:
{ "n" : 1, "ok" : 1 }
To override the default read concern level of 要覆盖"local"
, use the readConcern
option."local"
的默认读取关注级别,请使用readConcern
选项。
The following operation on a replica set specifies a Read Concern of 以下对副本集的操作将读取关注点指定为"majority"
to read the most recent copy of the data confirmed as having been written to a majority of the nodes."majority"
,以读取确认已写入大多数节点的数据的最新副本。
readConcern
level of "majority"
, you must specify a nonempty query
condition."majority"
的readConcern
级别,必须指定非空query
条件。db.runCommand( { count: "restaurants", query: { rating: { $gte: 4 } }, readConcern: { level: "majority" } } )
To ensure that a single thread can read its own writes, use 为了确保单个线程可以读取自己的写入,请对副本集的主线程使用"majority"
read concern and "majority"
write concern against the primary of the replica set."majority"
读取关注点和"majority"
写入关注点。