Guide to Data Aggregation in MongoDB: Examples and Use Cases
Certainly! Aggregation in MongoDB allows for transforming and combining documents in a collection to retrieve meaningful data insights. The aggregation pipeline framework is used for this purpose, where documents pass through a series of stages. Each stage performs an operation, such as filtering, grouping, or modifying the documents.
Here are a few examples of how aggregation might be used in MongoDB:
1. Basic Aggregation
Suppose you have a collection orders with documents that look like this:
{
"orderId": 1,
"customer": "John Doe",
"items": [
{"product": "item1", "quantity": 2, "price": 50},
{"product": "item2", "quantity": 1, "price": 30}
],
"totalPrice": 130,
"orderDate": "2023-10-01T10:00:00Z"
}
Example 1: Grouping and Summing Total Sales per Customer
To calculate the total sales made by each customer:
db.orders.aggregate([
{
"$group": {
"_id": "$customer",
"totalSales": {"$sum": "$totalPrice"}
}
}
])
This would produce an output where each document contains a customer and their corresponding total sales.
Example 2: Count Orders for Each Customer
To count the number of orders placed by each customer:
db.orders.aggregate([
{
"$group": {
"_id": "$customer",
"orderCount": {"$sum": 1}
}
}
])
This returns documents showing each customer with the number of orders they have placed.
Example 3: Projecting Specific Fields and Adding a New One
To project specific fields and add a new calculated field:
db.orders.aggregate([
{
"$project": {
"customer": 1,
"totalPrice": 1,
"orderId": 1,
"itemCount": {"$size": "$items"}
}
}
])
This will add a field itemCount to each document showing the number of items in each order.
Example 4: Filtering (Match Stage)
To fetch orders where the total price is greater than 100:
db.orders.aggregate([
{
"$match": {
"totalPrice": {"$gt": 100}
}
}
])
This filters the documents to include only those orders with a total price greater than 100.
Example 5: Sorting the Aggregated Results
To sort customers by their total sales in descending order:
db.orders.aggregate([
{
"$group": {
"_id": "$customer",
"totalSales": {"$sum": "$totalPrice"}
}
},
{
"$sort": {
"totalSales": -1
}
}
])
This returns the customers sorted by their total sales, from highest to lowest.
Example 6: Limiting Results
To limit the result set to the top 3 customers by total sales:
db.orders.aggregate([
{
"$group": {
"_id": "$customer",
"totalSales": {"$sum": "$totalPrice"}
}
},
{
"$sort": {
"totalSales": -1
}
},
{
"$limit": 3
}
])
This will return the top 3 customers based on total sales.
These are just a few examples of what you can do with MongoDB's aggregation framework. It is incredibly powerful and can be used to perform complex queries and data transformations.