Consider a MongoDB collection sales with documents like:
{ "item": "apple", "price": 10, "quantity": 5 }What will be the output of this pipeline?
db.sales.aggregate([
{ $match: { price: { $gt: 5 } } },
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } }
])db.sales.aggregate([
{ $match: { price: { $gt: 5 } } },
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } }
])Remember that $match filters documents before grouping.
The $match stage filters documents where price is greater than 5. Then $group sums the quantity for each item. Since the example document has price 10 and quantity 5, it is included and summed.
Given the same sales collection, what is the output of this pipeline?
db.sales.aggregate([
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" }, avgPrice: { $avg: "$price" } } },
{ $match: { avgPrice: { $gt: 5 } } }
])db.sales.aggregate([
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" }, avgPrice: { $avg: "$price" } } },
{ $match: { avgPrice: { $gt: 5 } } }
])Think about when filtering happens: before or after grouping.
This pipeline groups all documents first, calculating total quantity and average price per item. Then it filters groups where average price is greater than 5. Since the average price for "apple" is 10, it passes the filter.
Which of these MongoDB aggregation pipelines is inefficient because it processes more data than necessary?
Place $match before $sort to reduce the dataset early.
Option A sorts all documents by price before filtering with $match, processing unnecessary documents in the $sort stage. Option A filters first, making $sort more efficient. Options C and D use appropriate orders: grouping before sorting derived fields, and projecting before grouping is fine here.
Given this pipeline:
db.orders.aggregate([
{ $group: { _id: "$customer", totalSpent: { $sum: "$amount" } } },
{ $match: { amount: { $gt: 100 } } }
])Why does it return no results?
db.orders.aggregate([
{ $group: { _id: "$customer", totalSpent: { $sum: "$amount" } } },
{ $match: { amount: { $gt: 100 } } }
])Check the fields available after the $group stage.
After grouping, the documents have fields '_id' and 'totalSpent'. The field 'amount' no longer exists. So filtering on 'amount' in $match matches nothing, returning no results.
Which statement best explains why the order of stages in a MongoDB aggregation pipeline matters?
Think about filtering early vs late in the pipeline.
MongoDB runs pipeline stages in the order written. Early filtering reduces data for later stages, improving performance and changing results. The order affects both what data is processed and the final output.