定义
版本 5.0 中的新增功能。
返回群组中的文档数。
$count 可在以下阶段使用:
- $setWindowFields(从 MongoDB 5.0 开始提供)
语法
$count 事务语法:
{ $count: { } } 
$count 不接受任何参数。
行为
$count 在功能上等同于在 $group 阶段使用 { $sum : 1 }。
提示
示例
创建cakeSales集合,其中包含加利福尼亚州 ( CA ) 和华盛顿州 ( WA ) 的蛋糕销售情况:
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 阶段中使用
此示例在 $group 阶段使用 $count 来计算 cakeSales 集合中每个 state 的文档数量:
db.cakeSales.aggregate( [    {       $group: {          _id: "$state",          countNumberOfDocumentsForState: {             $count: {}          }       }    } ] ) 
在示例中:
- _id: "$state"按- state字段值对文档进行分组。其中有- CA和- WA的群组。
- $count: {}将- countNumberOfDocumentsForState字段设置为共享相同- state字段值的文档数量。
在此输出中,CA 和 WA 的文档数会显示在 countNumberOfDocumentsForState 字段中:
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 } { "_id" : "WA", "countNumberOfDocumentsForState" : 3 } 
在 $setWindowFields 阶段中使用
此示例在 $setWindowFields 阶段使用 $count 来计算窗口中定义的 cakeSales 集合中每个 state 的文档数量:
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下限与输出中- current文档之间的文档。这意味着- $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 }