0
0
MongoDBquery~20 mins

$sum accumulator in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate total sales per product
Given a collection sales with documents containing product and amount, what is the output of this aggregation pipeline?
MongoDB
db.sales.aggregate([
  { $group: { _id: "$product", totalSales: { $sum: "$amount" } } }
])
A[{ "_id": "apple", "totalSales": 150 }, { "_id": "banana", "totalSales": 200 }]
B[{ "_id": "apple", "totalSales": 350 }, { "_id": "banana", "totalSales": 200 }]
C[{ "_id": "apple", "totalSales": 150 }, { "_id": "banana", "totalSales": 100 }]
D[{ "_id": "apple", "totalSales": 0 }, { "_id": "banana", "totalSales": 0 }]
Attempts:
2 left
💡 Hint
Sum the amounts grouped by product name.
🧠 Conceptual
intermediate
1:30remaining
Understanding $sum with constant values
What does the following aggregation pipeline output if applied to any collection?
MongoDB
db.collection.aggregate([
  { $group: { _id: null, count: { $sum: 1 } } }
])
A[{ "_id": null, "count": <number_of_documents> }]
B[{ "_id": null, "count": 0 }]
C[{ "_id": null, "count": null }]
DSyntaxError
Attempts:
2 left
💡 Hint
Think about what summing 1 for each document means.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $sum usage
Which option contains a syntax error in using $sum inside a $group stage?
MongoDB
db.orders.aggregate([
  { $group: { _id: "$customer", total: { $sum: "price" } } }
])
AUsing $sum: "$price" (with $ before field name) is correct syntax.
BUsing $sum: ["$price"] is correct syntax.
CUsing $sum: { $price: 1 } is correct syntax.
DUsing $sum: "price" (without $) is correct syntax.
Attempts:
2 left
💡 Hint
Field names must be prefixed with $ inside $sum to refer to fields.
optimization
advanced
2:30remaining
Optimizing $sum with $match before $group
Which pipeline is more efficient to calculate total sales for products with amount > 100?
Adb.sales.aggregate([{ $group: { _id: "$product", total: { $sum: "$amount" } } }])
Bdb.sales.aggregate([{ $group: { _id: "$product", total: { $sum: "$amount" } } }, { $match: { total: { $gt: 100 } } }])
Cdb.sales.aggregate([{ $match: { amount: { $gt: 100 } } }, { $group: { _id: "$product", total: { $sum: "$amount" } } }])
Ddb.sales.aggregate([{ $group: { _id: "$product", total: { $sum: { $cond: [{ $gt: ["$amount", 100] }, "$amount", 0] } } } }])
Attempts:
2 left
💡 Hint
Filtering early reduces documents processed in grouping.
🔧 Debug
expert
3:00remaining
Why does this $sum aggregation return incorrect totals?
Given this pipeline, why might the total be incorrect?
MongoDB
db.orders.aggregate([
  { $group: { _id: "$customer", total: { $sum: "$items.price" } } }
])
ABecause $sum requires numeric literals, not field paths.
BBecause $sum cannot sum values inside arrays without $unwind first.
CBecause $group cannot use nested fields in _id.
DBecause $sum only works with top-level fields, not nested.
Attempts:
2 left
💡 Hint
Think about summing values inside arrays.