Common Use Cases: Grouping, Counting, Summing
The aggregation framework in MongoDB is a powerful tool that lets you transform and analyze data. One of the most common use cases involves grouping documents, counting them, and summing values across groups.
Why Use Aggregation?
Imagine you have a collection of sales data and you want to find:
- How many orders each customer placed
- Total sales per product
- Average quantity ordered by region
These kinds of operations require grouping and aggregating the data — something SQL does with GROUP BY
. In MongoDB, we use $group
and aggregation pipeline stages.
Sample Data Setup
Let’s use a sales
collection with the following documents:
db.sales.insertMany([
{ customer: "Alice", product: "Laptop", quantity: 1, amount: 70000 },
{ customer: "Bob", product: "Laptop", quantity: 1, amount: 70000 },
{ customer: "Alice", product: "Mouse", quantity: 2, amount: 1200 },
{ customer: "Charlie", product: "Laptop", quantity: 1, amount: 70000 },
{ customer: "Alice", product: "Keyboard", quantity: 1, amount: 2500 },
{ customer: "Bob", product: "Mouse", quantity: 1, amount: 600 }
]);
Use Case 1: Count Orders per Customer
We want to count how many orders each customer placed.
db.sales.aggregate([
{
$group: {
_id: "$customer",
totalOrders: { $sum: 1 }
}
}
]);
Output:
{ "_id": "Alice", "totalOrders": 3 } { "_id": "Bob", "totalOrders": 2 } { "_id": "Charlie", "totalOrders": 1 }
Explanation: We use $group
to group by customer
and count the documents in each group by adding $sum: 1
.
Use Case 2: Total Sales per Product
Now let’s calculate the total amount of money earned per product.
db.sales.aggregate([
{
$group: {
_id: "$product",
totalSales: { $sum: "$amount" }
}
}
]);
Output:
{ "_id": "Laptop", "totalSales": 210000 } { "_id": "Mouse", "totalSales": 1800 } { "_id": "Keyboard", "totalSales": 2500 }
Explanation: Here, we grouped by product
and summed the amount
field using $sum: "$amount"
.
Use Case 3: Total Quantity per Product
This example calculates how many units of each product were sold.
db.sales.aggregate([
{
$group: {
_id: "$product",
totalUnitsSold: { $sum: "$quantity" }
}
}
]);
Output:
{ "_id": "Laptop", "totalUnitsSold": 3 } { "_id": "Mouse", "totalUnitsSold": 3 } { "_id": "Keyboard", "totalUnitsSold": 1 }
Explanation: We grouped by product
and added up the quantity
for each.
Intuition Question
Q: What if a field (like quantity
) is missing in some documents?
A: If a field is missing, MongoDB treats it as null
, and $sum
will ignore it unless it’s explicitly 0 or a number. To avoid surprises, validate your data before aggregation.
Use Case 4: Count Sales by Customer and Product
We can group by a combination of fields using embedded documents in _id
.
db.sales.aggregate([
{
$group: {
_id: { customer: "$customer", product: "$product" },
totalOrders: { $sum: 1 }
}
}
]);
Output:
{ "_id": { "customer": "Alice", "product": "Mouse" }, "totalOrders": 1 } { "_id": { "customer": "Alice", "product": "Laptop" }, "totalOrders": 1 } { "_id": { "customer": "Alice", "product": "Keyboard" }, "totalOrders": 1 } { "_id": { "customer": "Bob", "product": "Laptop" }, "totalOrders": 1 } { "_id": { "customer": "Bob", "product": "Mouse" }, "totalOrders": 1 } { "_id": { "customer": "Charlie", "product": "Laptop" }, "totalOrders": 1 }
Explanation: By using an object in _id
, we create a grouped combination of customer and product. This helps answer "how many times did a customer buy a specific product?"
Summary
$group
is used to group documents by a field or fields$sum
is used for counting documents or adding numeric values- You can group by a single field or a combination of fields
These building blocks form the basis for more complex analytics with MongoDB’s aggregation pipeline.
Next Steps
In the next lesson, we’ll explore how to use $match
with $group
to filter data before aggregation.