On this page本页内容
$count
¶New in version 3.4.版本3.4中的新功能。
Passes a document to the next stage that contains a count of the number of documents input to the stage.将文档传递到下一个阶段,该阶段包含输入到该阶段的文档数计数。
$count
has the following prototype form:具有以下原型形式:
<string>
is the name of the output field which has the count as its value. 是以计数作为其值的输出字段的名称。<string>
must be a non-empty string, must not start with 必须是非空字符串,不得以$
and must not contain the .
character.$
开头,且不得包含.
字符。
The $count
stage is equivalent to the following $group
+ $project
sequence:$count
阶段相当于以下$group
+$project
序列:
where 其中myCount
would be the output field that contains the count. myCount
是包含计数的输出字段。You can specify another name for the output field.可以为输出字段指定另一个名称。
See also参阅
db.collection.countDocuments()
which wraps the $group
aggregation stage with a $sum
expression.db.collection.countDocuments()
,它用$sum
表达式包装$group
聚合阶段。
A collection named 名为scores的集合包含以下文档:scores
has the following documents:
The following aggregation operation has two stages:以下聚合操作分为两个阶段:
$match
stage excludes documents that have a score
value of less than or equal to 80
to pass along the documents with score
greater than 80
to the next stage.$match
阶段不包括得score
小于或等于80
的文档,以便将score
大于80
的文档传递到下一阶段。$count
stage returns a count of the remaining documents in the aggregation pipeline and assigns the value to a field called passing_scores
.$count
阶段返回聚合管道中剩余文档的计数,并将该值分配给名为passing_scores
的字段。The operation returns the following results:操作返回以下结果: