The $group
command in MongoDB's aggregation framework is a powerful tool for performing complex data analysis and summarization. It allows users to group documents based on specified keys and apply aggregate functions such as sum, count, average, min, max, and more.
In this article, we will explore MongoDB Aggregation $group
command in detail, covering its syntax, key functions, use cases, and examples to help beginners and professionals efficiently analyze their data
The $group
command is an important aggregation pipeline stage that enables grouping of documents and applying aggregate functions on the grouped data. It is commonly used for data analysis, reporting, and summarization. Along with basic aggregate functions like sum, count, and average the $group
supports a variety of other operations such as finding the maximum or minimum value in a group, concatenating strings and calculating standard deviations.
$group
Command
$sum
, $count
, $avg
, $max
, and $min
$match
, $sort
, and $project
Syntax:
The basic syntax of the $group command is as follows:
{
$group: {
_id: <expression>,
<field1>: { <accumulator1>: <expression1> },
<field2>: { <accumulator2>: <expression2> }
}
}
Key Terms
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:
[Example 1: Count the Number of Documents in a Collection
{
"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
}
]
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:
[Explanation:
{
"_id": null,
"count": 4
}
]
_id: null
→ Groups all documents together without a specific field.$sum: 1
→ Adds 1 for each document, effectively counting the total number of documents.sales
collectionThis 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.sales
collection, which are "Category 1"
and "Category 2"
.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:
[Explanation:
{ "_id": "Category 1", "totalAmount": 220 },
{ "_id": "Category 2", "totalAmount": 350 }
]
_id: "$category"
→ Groups documents by the category
field.$sum: "$amount"
→ Adds up the amount
values for each category.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.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.$group
in MongoDB
1. Use Indexing for Better Performance – Index fields used in grouping to speed up queries.
2. Optimize Aggregation Pipelines – Apply $match
before $group
to filter unnecessary documents.
3. Avoid Grouping on Large Fields – Avoid using large string fields for _id
to prevent memory overload.
4. Combine $group
with $sort
and $project
– Use $sort
for ordering results and $project
for refining output.
Overall, The $group
command in MongoDB's aggregation framework allow users to perform complex data manipulations and analytics efficiently. By using its capabilities, developers and data analysts can derive actionable insights from diverse datasets, enhancing decision-making processes and operational efficiencies. By mastering the $group
command, we can enhance our MongoDB data processing skills and build efficient data-driven applications.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4