Skip to main content

$group

The $group stage in MongoDB's aggregation pipeline is used to group documents by some specified expression and output a document for each distinct grouping. The grouped documents can then be processed using various accumulator operators to perform calculations such as summing, averaging, or finding the minimum and maximum values.

Basic Syntax

The basic syntax of the $group stage is as follows:

db.collection.aggregate([
{
$group: {
_id: <expression>, // Field for grouping
field1: { <accumulator1>: <expression1> },
field2: { <accumulator2>: <expression2> },
// ...
}
}
]);
  • _id: The field by which the documents will be grouped. This can be a single field or an expression.
  • field1, field2, ...: New fields that will contain the result of accumulator expressions.

Common Accumulator Operators

Here are some commonly used accumulator operators:

  • $sum: Sums up the numeric values of a field across all documents in a group.
  • $avg: Calculates the average of all the numeric values of a field in a group.
  • $min: Gets the minimum value of a field in a group.
  • $max: Gets the maximum value of a field in a group.
  • $push: Inserts the value to an array in the grouped document.
  • $first: Gets the first value in a group.
  • $last: Gets the last value in a group.

Examples

Simple Grouping

Grouping documents by the status field and calculating the total amount for each group:

db.orders.aggregate([
{
$group: {
_id: "$status",
totalAmount: { $sum: "$amount" }
}
}
]);

Complex Grouping

Grouping documents by multiple fields (status and customer), and calculating both the total and average amounts:

db.orders.aggregate([
{
$group: {
_id: { status: "$status", customer: "$customer" },
totalAmount: { $sum: "$amount" },
averageAmount: { $avg: "$amount" }
}
}
]);

Considerations

  1. Order of Documents: The $group stage does not guarantee the order of documents. Use $sort if you need sorted output.

  2. Memory Limitations: The $group stage has a limit on the amount of RAM it can consume. For larger datasets, you may need to enable disk usage or optimize your query.

  3. Type of _id: The _id field in the output of $group can be of any type, including an object or an array.

  4. Null and Missing Fields: If the field by which you're grouping is missing in a document, MongoDB will group that document under the _id of null.