![]() |
VOOZH | about |
The aggregation framework in MongoDB is a powerful feature that enables advanced data processing and analysis directly within the database. At the core of this framework is the aggregation pipeline, which allows you to transform and combine documents in a collection through a series of stages.
Table of Content
An aggregation pipeline consists of multiple stages, each performing an operation on the input documents. The output of one stage becomes the input for the next, allowing for complex data transformations and computations.
Let's walk through an example using a collection of sales data. Suppose we have the following documents in a sales collection:
[
{ "item": "apple", "quantity": 10, "price": 1.0, "date": "2023-05-01" },
{ "item": "banana", "quantity": 5, "price": 0.5, "date": "2023-05-01" },
{ "item": "apple", "quantity": 7, "price": 1.0, "date": "2023-05-02" },
{ "item": "banana", "quantity": 10, "price": 0.5, "date": "2023-05-02" }
]
We want to calculate the total sales for each item.
Filter documents to include only those with a quantity greater than 5:
{
$match: { quantity: { $gt: 5 } }
}
Group the documents by the item field and calculate the total quantity and total sales:
{
$group: {
_id: "$item",
totalQuantity: { $sum: "$quantity" },
totalSales: { $sum: { $multiply: ["$quantity", "$price"] } }
}
}
Sort the results by totalSales in descending order:
{
$sort: { totalSales: -1 }
}
Reshape the output documents to include only the fields item, totalQuantity, and totalSales:
{
$project: {
_id: 0,
item: "$_id",
totalQuantity: 1,
totalSales: 1
}
}
Combining these stages into a complete pipeline:
db.sales.aggregate([
{ $match: { quantity: { $gt: 5 } } },
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" }, totalSales: { $sum: { $multiply: ["$quantity", "$price"] } } } },
{ $sort: { totalSales: -1 } },
{ $project: { _id: 0, item: "$_id", totalQuantity: 1, totalSales: 1 } }
])
Output: The result of running this pipeline might be:
[
{ "item": "banana", "totalQuantity": 15, "totalSales": 7.5 },
{ "item": "apple", "totalQuantity": 17, "totalSales": 17.0 }
]
Aggregation pipelines in MongoDB provide a powerful and flexible way to process and analyze data. By chaining multiple stages, you can perform complex transformations and computations directly within the database. Understanding and utilizing these pipelines can greatly enhance your ability to work with MongoDB data, enabling advanced analytics and data processing capabilities.