Challenge - 5 Problems
Aggregation Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of the Aggregation Pipeline
Why do we use the aggregation pipeline in MongoDB?
Attempts:
2 left
💡 Hint
Think about how you can process data in stages to get meaningful results.
✗ Incorrect
The aggregation pipeline allows you to process data through multiple stages, like filtering, grouping, and calculating, to get complex results from your data.
❓ query_result
intermediate2:00remaining
Output of a Simple Aggregation Pipeline
Given a collection 'sales' with documents {item: 'apple', quantity: 5}, {item: 'banana', quantity: 10}, what is the output of this pipeline?
[{$match: {item: 'apple'}}, {$group: {_id: '$item', total: {$sum: '$quantity'}}}]
MongoDB
db.sales.aggregate([
{$match: {item: 'apple'}},
{$group: {_id: '$item', total: {$sum: '$quantity'}}}
])Attempts:
2 left
💡 Hint
Look for documents where item is 'apple' and sum their quantities.
✗ Incorrect
The pipeline first filters documents with item 'apple', then groups them by item and sums the quantity, resulting in total 5 for 'apple'.
📝 Syntax
advanced2:00remaining
Identify the Syntax Error in Aggregation Pipeline
Which option contains a syntax error in the aggregation pipeline?
MongoDB
db.orders.aggregate([
{$match: {status: 'shipped'}},
{$group: {_id: '$customerId', total: {$sum: '$amount'}}}
])Attempts:
2 left
💡 Hint
Check for missing or extra brackets.
✗ Incorrect
Option A is missing a closing bracket for the aggregate array, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing Aggregation Pipeline Performance
Which option best improves performance of an aggregation pipeline that filters and then groups data?
Attempts:
2 left
💡 Hint
Filtering early reduces the amount of data processed later.
✗ Incorrect
Filtering with $match first reduces the number of documents passed to $group, improving performance.
🔧 Debug
expert3:00remaining
Why Does This Aggregation Pipeline Return No Results?
Given this pipeline on collection 'products':
[{$match: {price: {$gt: 100}}}, {$group: {_id: '$category', avgPrice: {$avg: '$price'}}}]
Why might it return an empty result even though there are products with price > 100?
Attempts:
2 left
💡 Hint
Check the data type of the field used in the $match condition.
✗ Incorrect
If 'price' is stored as a string, numeric comparison with $gt fails, so no documents match the filter.