The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result. Each stage of the pipeline processes the documents as they pass along from one stage to another.
Pipeline operators provide tools to perform operations like sorting, grouping, and calculating aggregate values. Here are some common operators:
$match
- Filters the documents to pass only documents that match the specified condition(s) to the next pipeline stage.$group
- Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group.$sum
- Calculates and returns the sum of numeric values. $sum
can be used in $group
stage.$sort
- Reorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified.$match
and $group
// Consider a collection with the following documents
[
{ "_id": 1, "item": "apple", "price": 1, "quantity": 5 },
{ "_id": 2, "item": "banana", "price": 1.5, "quantity": 5 },
{ "_id": 3, "item": "apple", "price": 1, "quantity": 10 },
{ "_id": 4, "item": "banana", "price": 1.5, "quantity": 7 }
]
// Aggregation query
db.collection.aggregate([
{ $match: { item: "apple" } },
{
$group: {
_id: "$item",
total: { $sum: { $multiply: [ "$price", "$quantity" ] } }
}
}
])
// Here, $match filters out only documents where item is 'apple'.
// Then, $group groups these filtered documents by 'item' and calculates the total cost.
Expected Result:
[
{ "_id": "apple", "total": 15 }
]
You have learned the basic concepts of MongoDB aggregation and how to use different stages of the aggregation pipeline. Continue practicing these concepts and try out different aggregation operations.
Solutions:
db.students.aggregate([
{ $match: { age: { $gt: 15 } } },
{ $group: { _id: "$name", avgGrade: { $avg: "$grade" } } }
])
Here, we first filter students over the age of 15. Then, we group by name and calculate the average grade.
db.products.aggregate([
{ $group: { _id: "$product_name", totalValue: { $sum: { $multiply: ["$price", "$quantity"] } } } }
])
We group by product_name and calculate the total value of each product by multiplying price and quantity, then summing them up.
Keep practicing with different operators and complex queries to become proficient in MongoDB aggregation.