MongoDB Aggregation $group Command
Last Updated :
25 Sep, 2025
The $group command in MongoDB aggregation groups documents by a specified field and applies accumulator operators (like $sum, $avg, $max) to compute aggregated values for each group.
Features
- Allows grouping by multiple fields for detailed analysis.
- Can be combined with other stages such as $match, $sort, and $project.
- Efficiently summarizes large datasets.
Syntax:
{
$group: {
_id: <expression>,
<field1>: { <accumulator1>: <expression1> },
<field2>: { <accumulator2>: <expression2> }
}
}
In the above syntax:
- $_id -> The field used to group documents. It can be an existing field or a computed expression.
- <field1>, <field2> -> Fields to include in the output.
- <accumulator1>, <accumulator> -> Aggregate functions to apply to grouped data.
- <expression>, <expression> -> Expressions to compute values for grouping or aggregation.
Examples of $group Command in MongoDB
The $group
command is widely used for aggregating and analyzing data in MongoDB. It helps in summarizing sales, counting occurrences, and computing statistics efficiently. To illustrate its usage, let's consider a sales
collection that stores sales transactions, where each document includes details such as product
, category
, and amount
. Below is a sample dataset:
Sample Data:
[
{
"product": "Product A",
"category": "Category 1",
"amount": 100
},
{
"product": "Product B",
"category": "Category 2",
"amount": 150
},
{
"product": "Product C",
"category": "Category 1",
"amount": 120
},
{
"product": "Product D",
"category": "Category 2",
"amount": 200
}
]
Example 1: Count the Number of Documents in a Collection
This query calculates the total number of documents present in the sales
collection, providing a quick way to determine the dataset size.
Query:
db.sales.aggregate([
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
])
Output:
[
{
"_id": null,
"count": 4
}
]
Explanation:
_id: null
→ Groups all documents together without a specific field.$sum: 1
→ Adds 1 for each document, effectively counting the total number of documents.- The result shows that there are 4 documents in the
sales
collection
Example 2. Retrieve Distinct Values
This query retrieves unique category values from the sales
collection, helping identify different product categories available in the dataset.
Query:
db.sales.aggregate([
{
$group: {
_id: "$category"
}
}
])
Output:
[
{ "_id": "Category 1" },
{ "_id": "Category 2" }
]
Explanation:
_id: "$category"
→ Groups documents by the category
field, effectively extracting distinct category values.- The result lists the unique categories present in the
sales
collection, which are "Category 1"
and "Category 2"
. - This approach is useful for filtering unique values in large datasets efficiently.
Example 3: Group by Item Having
This query groups documents by category and calculates the total sales amount for each category in the sales
collection
Query:
db.sales.aggregate([
{
$group: {
_id: "$category",
totalAmount: { $sum: "$amount" }
}
}
])
Output:
[
{ "_id": "Category 1", "totalAmount": 220 },
{ "_id": "Category 2", "totalAmount": 350 }
]
Explanation:
_id: "$category"
→ Groups documents by the category
field.$sum: "$amount"
→ Adds up the amount
values for each category.- The result shows that Category 1 has a total sales amount of 220, while Category 2 has 350.
- This query is useful for financial analysis, revenue tracking, and sales reporting
Example 4: Calculate Count, Sum, and Average
This query groups documents by category and calculates the total count of documents, sum of sales amount, and average sales amount per category in the sales
collection.
Query:
db.sales.aggregate([
{
$group: {
_id: "$category",
count: { $sum: 1 },
totalAmount: { $sum: "$amount" },
averageAmount: { $avg: "$amount" }
}
}
])
Output:
[
{
"_id": "Category 1",
"count": 2,
"totalAmount": 220,
"averageAmount": 110
},
{
"_id": "Category 2",
"count": 2,
"totalAmount": 350,
"averageAmount": 175
}
]
Explanation:
_id: "$category"
→ Groups documents by category.$sum: 1
→ Counts the number of documents in each category.$sum: "$amount"
→ Computes the total sales amount per category.$avg: "$amount"
→ Calculates the average sales amount per category.- The result shows that Category 1 has 2 transactions, with a total amount of 220 and an average amount of 110, while Category 2 has 2 transactions, with a total amount of 350 and an average amount of 175.
Exampl 5: Group by null
This query calculates the total sum of the amount
field across all documents in the sales
collection, without grouping by any specific field.
Query:
db.sales.aggregate([
{
$group: {
_id: null,
totalAmount: { $sum: "$amount" }
}
}
])
Output:
[
{ "_id": null, "totalAmount": 570 }
]
Explanation:
_id: null
→ Groups all documents together as a single group, meaning the entire collection is aggregated.$sum: "$amount"
→ Computes the total sum of the amount
field across all documents.- The output shows that the total sales amount in the collection is 570.
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators