mongo-db
  1. mongo-db-group-operator

$group operator - ( MongoDB Misc )

In MongoDB, the $group operator is used to group documents together into subsets based on one or more fields. The $group operator is commonly used in the aggregation pipeline to perform operations like sum, average, minimum, maximum, and count on these grouped documents.

Syntax

The $group operator has the following syntax:

{
   $group: {
      _id: <expression>,
      <field1>: { <accumulator1> : <expression1> },
      ...
   }
}

Here, the _id field is used to specify the field that is used to group the documents. The <field> parameter is the field that will be operated on, and the <expression> parameter is the expression to be used for aggregation. Multiple fields can be included in a single $group operator, separated by a comma.

Example

Suppose you have a collection named orders with the following documents:

{
   _id : 1,
   item: "apple",
   price: 0.5,
   quantity: 10
},
{
   _id : 2,
   item: "banana",
   price: 0.25,
   quantity: 20
},
{
   _id : 3,
   item: "orange",
   price: 0.75,
   quantity: 8
}

To group these documents by the item field and calculate the total quantity and total price for each item, you could use the following $group operator:

db.orders.aggregate([
    { $group: {
        _id: "$item",
        totalQuantity: { $sum: "$quantity" },
        totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } }
    } }
])

The $sum operator is used to calculate the total quantity and total price for each item.

Output

The output of the above example would be:

{
  "_id" : "orange",
  "totalQuantity" : 8,
  "totalPrice" : 6
},
{
  "_id" : "banana",
  "totalQuantity" : 20,
  "totalPrice" : 5
},
{
  "_id" : "apple",
  "totalQuantity" : 10,
  "totalPrice" : 5
}

Explanation

The $group operator is used to group the documents by the item field and calculate the total quantity and total price for each item. The totalQuantity field is calculated using the $sum operator, which returns the sum of the quantity field for all documents in the group. The totalPrice field is calculated by multiplying the price and quantity fields for each document using the $multiply operator, then summing the results with the $sum operator.

Use

The $group operator is useful for performing aggregation operations on grouped documents in MongoDB. It allows you to group documents together based on one or more fields and perform various operations like sum, average, minimum, maximum, and count on the grouped documents.

Important Points

  • The $group operator is used to group documents together into subsets based on one or more fields.
  • The _id field is used to specify the field that is used to group the documents.
  • The $sum operator is commonly used in conjunction with the $group operator to perform aggregation operations on grouped documents.

Summary

In this page, we discussed the $group operator in MongoDB. We covered the syntax, example, output, explanation, use, important points, and summary of the $group operator. By using the $group operator in conjunction with other aggregation operators, you can perform a wide range of aggregation operations on grouped documents in MongoDB.

Published on: