$count (aggregation accumulator)

On this page本页内容

Definition定义

New in version 5.0.在版本5.0中新增

$count

Returns the number of documents in a group.返回组中的文档数。

$count is available in these stages:在以下阶段可用:

Note注意
Disambiguation消除歧义

This page describes the $count aggregation accumulator. 本页介绍$count聚合累加器。For the $count aggregation pipeline stage, see $count (aggregation pipeline).有关$count聚合管道阶段,请参阅$count(聚合管道)。

Syntax语法

$count syntax:语法:

{ $count: { } }

$count does not accept any parameters.不接受任何参数。

Behavior行为

$count is functionally equivalent to using { $sum : 1 } within the $group stage.在功能上等同于在$group阶段中使用{ $sum : 1 }

Tip提示
See also: 参阅:

Examples示例

Create a cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):创建包含加利福尼亚州(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 }
] )

The cakeSales collection is used in the following examples.以下示例中使用了cakeSales集合。

Use in $group Stage$group阶段中使用

This example uses $count in the $group stage to count the number of documents in the cakeSales collection for each state:本例使用$group阶段中的$count计算每个状态的cakeSales集合中的文档数:

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

In the example:在该示例中:

  • _id: "$state" groups the documents by the state field value. state字段值对文档进行分组。There are groups for CA and WA.CAWA组。
  • $count: {} sets the countNumberOfDocumentsForState field to the number of documents that share the same state field value.countNumberOfDocumentsForState字段设置为共享相同state字段值的文档数。

In this output, the number of documents for CA and WA is shown in the countNumberOfDocumentsForState field:在此输出中,CAWA的文档数显示在countNumberOfDocumentsForState字段中:

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

Use in $setWindowFields Stage$setWindowFields阶段中使用

This example uses $count in the $setWindowFields stage to count the number of documents in the cakeSales collection for each state defined in the window:本例使用$setWindowFields阶段中的$count来计算窗口中定义的每个statecakeSales集合中的文档数:

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

In the example:在该示例中:

  • partitionBy: "$state" partitions the documents in the collection by state. state对集合中的文档进行分区There are partitions for CA and WA.有用于CAWA的分区。
  • sortBy: { orderDate: 1 } sorts the documents in each partition by orderDate in ascending order (1), so the earliest orderDate is first.orderDate按升序(1)对每个分区中的文档进行排序,因此最早的orderDate是第一个。
  • output sets the countNumberOfDocumentsForState field to the number of documents using $count that is run in a documents window.countNumberOfDocumentsForState字段设置为使用在文档窗口中运行的$count的文档数。

    The window contains documents between an unbounded lower limit and the current document in the output. 窗口包含的文档位于unbounded下限和输出中的current文档之间。This means $count returns the number of documents between the beginning of the partition and the current document.这意味着$count返回分区开始和当前文档之间的文档数。

In this output, the number of documents for CA and WA is shown in the countNumberOfDocumentsForState field:在此输出中,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 (aggregation)$covariancePop (aggregation) →