Docs 菜单

Docs 主页开发应用程序MongoDB Manual

$count(聚合累加器)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例

版本 5.0 中的新增功能

$count

返回群组中的文档数。

$count 可在以下阶段使用:

注意

消歧

本页介绍$count聚合累加器。有关$count聚合管道阶段,请参阅$count (aggregation pipeline)

$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 集合。

此示例在 阶段使用$count $group来计算cakeSales 集合中每个state 的文档数量:

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )

在示例中:

  • _id: "$state"state 字段值对文档进行分组。其中有 CAWA 的群组。

  • $count: {}countNumberOfDocumentsForState 字段设置为共享相同 state 字段值的文档数量。

在此输出中,CAWA 的文档数会显示在 countNumberOfDocumentsForState 字段中:

{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }

此示例在 阶段使用$count $setWindowFields来计算cakeSales state窗口 中定义的每个 的 集合中的文档数量:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
countNumberOfDocumentsForState: {
$count: {},
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

在示例中:

  • partitionBy: "$state"state 对集合中的文档分区CAWA 都有分区。

  • sortBy: { orderDate: 1 }orderDate 以升序 (1) 对每个分区中的文档进行排序,因此最早的 orderDate 位于最前面。

  • output 使用在countNumberOfDocumentsForState $count文档 窗口中运行的 将 字段设置为文档数。

    窗口包含介于unbounded下限和输出中current文档之间的文档。这意味着$count会返回分区开头和当前文档之间的文档数。

在此输出中,CAWA 的文档数会显示在 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 }
← $cosh(聚合)