Mongodb $group运算符

Mongodb $group运算符

$group运算符在MongoDB中非常关键,因为它可以帮助进行各种数据转换。$group运算符通过一定的指定表达式对相似的数据进行分组,并且为每个不同的分组分组文档。

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

语法:

{
 $group:
     {
          _id: <expression>, // Group By Expression
          <field1>: { <accumulator1> : <expression1> },
           ...
      }
}

重要点:

  1. _id:此字段是用于分组的必选字段。如果您将_id字段的值指定为null或常量,则$group运算符将累加所有输入文档的值作为一个整体进行计算。
  2. field:这是一个可选字段,可以使用运算符对其进行计算。

示例

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

Database: JavaTpoint
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:按多个键进行分组

在这个示例中,我们将按照账单日期和商品名称字段进行分组,并显示这些字段(总价格、平均数量以及同一日期的账单数量)。

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

在这个示例中,我们将通过账单日期和项目名称字段进行分组,并为账单日期为05/12/2014的文档显示这些字段(总价、平均数量以及相同日期的账单数量)。

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
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程