文档菜单
文档首页
/
MongoDB 手册
/ / /

$count(聚合累加器)

本页内容

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

版本5.0.

$count

返回组中文档的数量。

$count在以下阶段可用

  • $bucket

  • $bucketAuto

  • $group

  • $setWindowFields(从 MongoDB 5.0 开始可用)

注意

区分

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

$count语法

{ $count: { } }

$count不接受任何参数。

$count 在功能上等同于在 $group 阶段中使用 { $sum : 1 }

提示

另请参阅

创建一个包含加利福尼亚州(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 集合。

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

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

在示例中

  • _id: "$state" 根据字段值对文档进行分组。有针对 CAWA 的组。

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

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

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

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

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 是第一。

  • outputcountNumberOfDocumentsForState 字段设置为使用 $count文档 窗口中运行的文档数量。

    窗口 包含从 unbounded 下限到输出中当前文档之间的文档。这意味着 $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