The MongoDB Aggregation Framework is a powerful tool for data aggregation, transformation, and analysis. It allows you to process data records and return computed results. This framework is highly flexible and can handle complex data processing tasks efficiently.
The Aggregation Framework in MongoDB processes documents through a series of stages, each stage transforming the input documents into an output document that is passed on to the next stage. The pipeline consists of one or more stages, where each stage performs a specific operation on the documents.
Suppose you have a collection named orders with the following documents:
{ "_id" : 1, "item" : "abc", "quantity" : 2, "price" : 10 }
{ "_id" : 2, "item" : "xyz", "quantity" : 1, "price" : 5 }
{ "_id" : 3, "item" : "abc", "quantity" : 4, "price" : 10 }
To calculate the total revenue for each item:
db.orders.aggregate([
{
$group: {
_id: "$item",
totalRevenue: { $sum: { $multiply: ["$quantity", "$price"] } }
}
},
{
$sort: { totalRevenue: -1 }
}
])
Explanation:
item field and calculates the total revenue using $sum with a multiplication of quantity and price.totalRevenue.To find orders where the quantity is greater than 1:
db.orders.aggregate([
{
$match: { quantity: { $gt: 1 } }
},
{
$group: {
_id: "$item",
totalQuantity: { $sum: "$quantity" }
}
}
])
Explanation:
quantity is greater than 1.item and sums up the quantity.Suppose you have a collection named sales with documents like:
{ "_id" : 1, "product" : "Widget", "sales": [ { "region": "North", "amount": 200 }, { "region": "South", "amount": 300 } ] }
{ "_id" : 2, "product" : "Gadget", "sales": [ { "region": "East", "amount": 400 } ] }
To calculate total sales per region:
db.sales.aggregate([
{
$unwind: "$sales"
},
{
$group: {
_id: "$sales.region",
totalSales: { $sum: "$sales.amount" }
}
}
])
Explanation:
sales array field from the input documents to output a document for each element.region and sums up the amount.$match, $sort, and $group stages are indexed to improve performance.$limit and $skip for pagination, especially with large datasets.The MongoDB Aggregation Framework is a versatile tool for data analysis and transformation. By understanding the basic stages and best practices, you can effectively use it to process and analyze your data efficiently. Whether you're calculating totals, filtering documents, or reshaping data, the Aggregation Framework provides the necessary tools to meet your requirements.
For more advanced features and detailed documentation, refer to the official MongoDB Aggregation Framework documentation.