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.返回包含此计数和命令状态的文档。
Note
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:具有以下形式:
Note
Starting in version 4.2, MongoDB implements a stricter validation of the option names for the 从4.2版开始,MongoDB对count
command. count
命令的选项名实施了更严格的验证。The command now errors if you specify an unknown option name.如果指定未知的选项名称,则该命令现在会出错。
count
has the following fields:具有以下字段:
count |
string | |
query |
document | |
limit |
integer | |
skip |
integer | |
hint |
string or document | |
readConcern |
document |
For more formation on the read concern levels, see Read Concern Levels. |
collation |
document | Optional.
The collation option
If the collation is unspecified but the collection has a default collation (see
|
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
The mongo
shell also provides the following wrapper methods for count
:
Important
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. In particular,You cannot use count
and shell helpers count()
and db.collection.count()
in transactions.
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.
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:
See also参阅
$collStats
to return an approximate count based on the collection’s metadata.
After an unclean shutdown of a mongod
using the Wired Tiger storage engine, count statistics reported by count
may be inaccurate.
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. However, mongod
instances running with non-default --syncdelay
settings may have more or less frequent checkpoints.
Run validate
on each collection on the mongod
to restore the correct statistics after an unclean shutdown.
Note
This loss of accuracy only applies to count
operations that do not include a query document.
The following sections provide examples of the count
command.
The following operation counts the number of all documents in the 以下操作统计orders
collection:orders
集合中所有文档的数量:
In the result, the n
, which represents the count, is 26
, and the command status ok
is 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')
的文档计数:
In the result, the n
, which represents the count, is 13
and the command status ok
is 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:
In the result, the n
, which represents the count, is 3
and the command status ok
is 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"
:
In the result, the 结果中,代表计数的n
, which represents the count, is 1
and the command status ok
is 1
:n
为1
,命令状态ok
为1
:
To override the default read concern level of "local"
, use the readConcern
option.
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.
Important
"majority"
, replica sets must use WiredTiger storage engine.
You can disable read concern "majority"
for a deployment with a three-member primary-secondary-arbiter (PSA) architecture;
however, this has implications for change streams (in MongoDB 4.0 and earlier only) and transactions on sharded clusters. For more information, see Disable Read Concern Majority.
readConcern
level of "majority"
, you must specify a nonempty query
condition.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.