MongoDB $group 运算符

MongoDB $group 运算符 介绍

$group 运算符在 MongoDB 中至关重要,因为它有助于执行各种数据转换。$group 运算符通过某些指定的表达式对相似的数据进行分组,并为每个不同的分组对文档进行分组。

假设数据库中有 50 名学生,他们都喜欢板球。如果我们想统计所有喜欢板球的学生,那么 $group 运算符是完成此类任务的优雅解决方案。

MongoDB $group 运算符 语法

{  
 $group:  
     {  
          _id: <expression>, // Group By Expression  
          <field1>: { <accumulator1> : <expression1> },  
           ...  
      }  
}  
  1. _id:此字段对于分组是必需的。如果您将 _id 字段的值指定为 null 或常量,则 $group 运算符会将所有输入文档的累积值作为一个整体进行计数。
  2. field:这是一个可选字段,使用 <accumulator> 运算符计算。

MongoDB $group 运算符 例子

在以下示例中,我们正在使用:

Database: Yiidian
Collection: invoice  
Document: Ten documents that contain the details of the invoice  
{
        "_id" : A1,
        "item_name" : "Blue box",
        "price" : 10,
        "qty" : 15,
        "date_of_bill" : "13/04/2015"
}
{
        "_id" : A2,
        "item_name" : "Light Red box",
        "price" : 15,
        "qty" : 20,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : null,
        "item_name" : "Green box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "17/12/2014"
}
{
        "_id" : A3,
        "item_name" : "White box",
        "price" : 8,
        "qty" : 25,
        "date_of_bill" : "07/02/2014"
}
{
        "_id" : A4,
        "item_name" : "Blue box",
        "price" : 15,
        "qty" : 20,
        "date_of_bill" : "13/04/2015"
}
{
        "_id" : A5,
        "item_name" : "Red box",
        "price" : 12,
        "qty" : 10,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : A6,
        "item_name" : "Black box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "22/04/2020"
}
{
        "_id" : A7,
        "item_name" : "Red box",
        "price" : 8,
        "qty" : 15,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : A8,
        "item_name" : "Green box",
        "price" : 20,
        "qty" : 10,
        "date_of_bill" : "17/12/2014"
}
{
        "_id" : A9,
        "item_name" : "Green box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "17/12/2014"
}

示例 1:$group

在此示例中,我们将按账单日期分组并显示这些字段(总价、平均数量和计算同一日期的账单数量)。

db.invoice.aggregate(  
   [  
      {  
        $group : {_id : "$date_of_bill ",  
           Total_price: { $sum: { $multiply: [ "$price", "$qty" ] } },  
           Average_qty: { $avg: "$qty" },  
           count: { $sum: 1 }  
        }  
     }  
  ]  
).pretty();  

输出结果为:

{ "_id" : "13/04/2015", "Total_price" : 875, "Average_qty" : 17.5, "count" : 2 }
{ "_id" : "05/12/2014", "Total_price" : 1575, "Average_qty" : 15, "count" : 3 }
{ "_id" : "17/12/2014", "Total_price" : 2800, "Average_qty" : 28.3333333, "count" : 3 }
{ "_id" : "07/02/2014", "Total_price" : 200, "Average_qty" : 25, "count" : 1 }
{ "_id" : "22/04/2020", "Total_price" : 300, "Average_qty" : 30, "count" : 1 }

此处的结果显示字段账单日期文档已分组,并显示了该日期的总价、平均数量和完成的账单数量。

示例 2:多个属性上的 $group

在此示例中,我们将按账单日期和项目名称字段分组并显示这些字段(总价、平均数量和计算同一日期的账单数量)。

db.invoice.aggregate(  
   [  
      {  
        $group : {_id : { date_of_bill : "$date_of_bill ", item : "$item_name"  
           Total_price: { $sum: { $multiply: [ "$price", "$qty" ] } },  
           Average_qty: { $avg: "$qty" },  
           count: { $sum: 1 }  
        }  
     }  
  ]  
).pretty();

输出结果为:

{ "_id" : { 
            "date_of_bill" : "13/04/2015", 
            "item" : "Blue box"
          }
          "Total_price" : 875, 
          "Average_qty" : 17.5, 
          "count" : 2 
}
{ "_id" : { 
            "date_of_bill" : "05/12/2014", 
            "item" : "Light Red box"
          }
          "Total_price" : 300, 
          "Average_qty" : 20, 
          "count" : 1
}

{ "_id" : { 
            "date_of_bill" : "05/12/2014", 
            "item" : "Red box"
          }
          "Total_price" : 500, 
          "Average_qty" : 12.5, 
          "count" : 2
}
{ "_id" : { 
            "date_of_bill" : "17/12/2014", 
            "item" : "Green box"
          }
          "Total_price" : 2800, 
          "Average_qty" : 28.3333333, 
          "count" : 3
}
{ "_id" : { 
            "date_of_bill" : "07/02/2014", 
            "item" : "White box"
          }
          "Total_price" : 200, 
          "Average_qty" : 25, 
          "count" : 1
}
{ "_id" : { 
            "date_of_bill" : "22/04/2020", 
            "item" : "Black box"
          }
          "Total_price" : 300, 
          "Average_qty" : 30, 
          "count" : 1
}

示例 3:在多个属性上使用 $match 的 $group

在此示例中,我们将按账单日期和项目名称字段分组,并为账单日期为 2014 年 5 月 12 日的文档显示这些字段(总价、平均数量和计算同一日期的账单数量)。

db.invoice.aggregate(  
   [  
    {   
    $match : { date_of_bill : "05/12/2014"}  
    },  
      {  
           $group : {_id : { date_of_bill : "$date_of_bill ", item : "$item_name"  
           Total_price: { $sum: { $multiply: [ "$price", "$qty" ] } },  
           Average_qty: { $avg: "$qty" },  
           count: { $sum: 1 }  
        }  
     }  
  ]  
).pretty();  

输出结果为:

{ "_id" : { 
            "date_of_bill" : "05/12/2014", 
            "item" : "Light Red box"
          }
          "Total_price" : 300, 
          "Average_qty" : 20, 
          "count" : 1
}

{ "_id" : { 
            "date_of_bill" : "05/12/2014", 
            "item" : "Red box"
          }
          "Total_price" : 500, 
          "Average_qty" : 12.5, 
          "count" : 2
}

 

热门文章

优秀文章