0
0
MongoDBquery~20 mins

$group stage for aggregation in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of $group with sum accumulator
Given the collection 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" } } }])
A[{ "_id": "apple", "totalQty": 20 }, { "_id": "banana", "totalQty": 10 }]
B[{ "_id": "apple", "totalQty": 5 }, { "_id": "banana", "totalQty": 10 }, { "_id": "apple", "totalQty": 15 }]
C[{ "_id": null, "totalQty": 30 }]
D[{ "_id": "apple", "totalQty": 15 }, { "_id": "banana", "totalQty": 10 }]
Attempts:
2 left
💡 Hint
Think about grouping by the item name and summing quantities for each group.
📝 Syntax
intermediate
1: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" } } }])
A{ $group: { _id: "$customer", total: { $sum: "$amount" } } }
B{ $group: { _id: "$customer", total: { $sum: amount } } }
C} } } "tnuoma$" :mus$ { :latot ,"remotsuc$" :di_ { :puorg$ {
D $group: { _id: "$customer", total: { $sum: "$amount" } } }
Attempts:
2 left
💡 Hint
Check if the field names are properly quoted as strings.
query_result
advanced
2:30remaining
Output of $group with multiple accumulators
Given the collection 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" } } }])
A[{ "_id": "fruit", "count": 2, "avgPrice": 12.5 }, { "_id": "vegetable", "count": 1, "avgPrice": 20 }]
B[{ "_id": "fruit", "count": 1, "avgPrice": 12.5 }, { "_id": "vegetable", "count": 1, "avgPrice": 20 }]
C[{ "_id": "fruit", "count": 2, "avgPrice": 25 }, { "_id": "vegetable", "count": 1, "avgPrice": 20 }]
D[{ "_id": null, "count": 3, "avgPrice": 15 }]
Attempts:
2 left
💡 Hint
Count counts documents per group, avgPrice averages the price field per group.
🔧 Debug
advanced
2:00remaining
Why does this $group aggregation return empty results?
Consider this aggregation on collection 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?
AThe field "count" does not exist in documents, so $sum returns null and no groups form.
BThe aggregation pipeline is missing a $match stage to filter documents.
CThe field "count" is a string, so $sum fails silently and returns empty.
DThe aggregation is correct; empty output means no documents in collection.
Attempts:
2 left
💡 Hint
Consider if there are actually documents in the collection matching the pipeline.
🧠 Conceptual
expert
1:30remaining
Understanding $group _id field behavior
In MongoDB aggregation, what happens if you set _id to null in the $group stage?

Choose the correct explanation:
ADocuments are grouped by a field named "null", which usually does not exist, so output is empty.
BEach document is grouped by its own _id field, so output has same number of documents as input.
CAll documents are grouped into a single group, so the output has one document aggregating all input documents.
DThe aggregation fails with an error because _id cannot be null in $group.
Attempts:
2 left
💡 Hint
Think about grouping all documents together regardless of any field.