$count(聚合累加器)
定义
新版本5.0.
返回组中文档的数量。
$count
在以下阶段可用
$setWindowFields
(从 MongoDB 5.0 开始可用)
语法
$count
语法
{ $count: { } }
$count
不接受任何参数。
行为
示例
创建一个包含加利福尼亚州(CA
)和华盛顿州(WA
)糕点销售的 cakeSales
集合
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
以下示例使用了 cakeSales
集合。
在 $group
阶段中使用
此示例在 $count
阶段中使用,用于计算 cakeSales
集合中每个 state
字段的文档数量
db.cakeSales.aggregate( [ { $group: { _id: "$state", countNumberOfDocumentsForState: { $count: {} } } } ] )
在示例中
_id: "$state"
根据字段值对文档进行分组。有针对CA
和WA
的组。$count: {}
将countNumberOfDocumentsForState
字段设置为具有相同state
字段值的文档数量。
在此输出中,CA
和 WA
的文档数量显示在 countNumberOfDocumentsForState
字段中
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 } { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
在 $setWindowFields
阶段使用
此示例在 $count
中使用 $setWindowFields
阶段,以计算每个在 窗口 中定义的 state
的 cakeSales
集合中的文档数量:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { countNumberOfDocumentsForState: { $count: {}, window: { documents: [ "unbounded", "current" ] } } } } } ] )
在示例中
partitionBy: "$state"
分区 集合中的文档,根据state
。有CA
和WA
的分区。sortBy: { orderDate: 1 }
排序 每个分区中的文档,按orderDate
降序排列(1
),因此最早的orderDate
是第一。
output
将countNumberOfDocumentsForState
字段设置为使用$count
在 文档 窗口中运行的文档数量。此 窗口 包含从
unbounded
下限到输出中当前文档之间的文档。这意味着$count
返回从分区开始到当前文档之间的文档数量。
在此输出中,CA
和 WA
的文档数量显示在 countNumberOfDocumentsForState
字段中
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "countNumberOfDocumentsForState" : 1 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "countNumberOfDocumentsForState" : 2 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "countNumberOfDocumentsForState" : 3 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "countNumberOfDocumentsForState" : 1 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "countNumberOfDocumentsForState" : 2 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "countNumberOfDocumentsForState" : 3 }