Challenge - 5 Problems
MongoDB Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of $group with sum accumulator
Given the collection
What is the output of this aggregation?
sales with documents:{ "item": "apple", "qty": 5 }
{ "item": "banana", "qty": 10 }
{ "item": "apple", "qty": 15 }What is the output of this aggregation?
db.sales.aggregate([{ $group: { _id: "$item", totalQty: { $sum: "$qty" } } }])MongoDB
db.sales.aggregate([{ $group: { _id: "$item", totalQty: { $sum: "$qty" } } }])Attempts:
2 left
💡 Hint
Think about grouping by the item name and summing quantities for each group.
✗ Incorrect
The $group stage groups documents by the _id field, here the item name. Then it sums the qty values for each group. Apples have qty 5 + 15 = 20, bananas have qty 10.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in $group stage
Which option contains a syntax error in the $group stage of this aggregation pipeline?
db.orders.aggregate([{ $group: { _id: "$customer", total: { $sum: "$amount" } } }])Attempts:
2 left
💡 Hint
Check if the field names are properly quoted as strings.
✗ Incorrect
In option B, $sum is given amount without quotes, which is invalid. Field names must be strings with $ prefix inside quotes.
❓ query_result
advanced2:30remaining
Output of $group with multiple accumulators
Given the collection
What is the output of this aggregation?
products with documents:{ "category": "fruit", "price": 10 }
{ "category": "fruit", "price": 15 }
{ "category": "vegetable", "price": 20 }What is the output of this aggregation?
db.products.aggregate([{ $group: { _id: "$category", count: { $sum: 1 }, avgPrice: { $avg: "$price" } } }])MongoDB
db.products.aggregate([{ $group: { _id: "$category", count: { $sum: 1 }, avgPrice: { $avg: "$price" } } }])Attempts:
2 left
💡 Hint
Count counts documents per group, avgPrice averages the price field per group.
✗ Incorrect
The $group groups by category. For fruit, two documents with prices 10 and 15 average to 12.5 and count is 2. Vegetable has one document with price 20.
🔧 Debug
advanced2:00remaining
Why does this $group aggregation return empty results?
Consider this aggregation on collection
But the output is an empty array. The documents look like:
What is the most likely reason for empty output?
logs:db.logs.aggregate([{ $group: { _id: "$level", total: { $sum: "$count" } } }])But the output is an empty array. The documents look like:
{ "level": "error", "count": 5 }
{ "level": "info", "count": 3 }What is the most likely reason for empty output?
Attempts:
2 left
💡 Hint
Consider if there are actually documents in the collection matching the pipeline.
✗ Incorrect
Empty output from an aggregation pipeline with no filtering stages ($match, etc.) indicates that the collection has no documents. Even if fields are missing, $group would output groups with accumulators initialized to 0 or null.
🧠 Conceptual
expert1:30remaining
Understanding $group _id field behavior
In MongoDB aggregation, what happens if you set
Choose the correct explanation:
_id to null in the $group stage?Choose the correct explanation:
Attempts:
2 left
💡 Hint
Think about grouping all documents together regardless of any field.
✗ Incorrect
Setting _id to null groups all documents into one group, producing a single output document with aggregated values over the entire collection.