Challenge - 5 Problems
MongoDB Pre-Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a pre-aggregation computed field
Given a MongoDB collection
sales with documents containing price and quantity, what is the output of this aggregation pipeline?MongoDB
db.sales.aggregate([
{
$project: {
totalSale: { $multiply: ["$price", "$quantity"] },
item: 1
}
}
])Attempts:
2 left
💡 Hint
Remember that $multiply computes the product of numeric fields.
✗ Incorrect
The $project stage computes a new field totalSale by multiplying price and quantity for each document, preserving the item field.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in computed pre-aggregation
Which option contains a syntax error in the MongoDB aggregation pipeline that computes a new field?
MongoDB
db.orders.aggregate([
{
$project: {
total: { $add: ["$price", "$tax"] },
item: 1
}
}
])Attempts:
2 left
💡 Hint
Check the use of $add operator and field quoting.
✗ Incorrect
The $add operator accepts an array of expressions, and field names are correctly quoted as strings with $ prefix. The pipeline syntax is valid.
❓ optimization
advanced2:30remaining
Optimize pre-aggregation for repeated computed fields
You want to optimize a MongoDB aggregation pipeline that computes the same complex field multiple times. Which approach is best?
Attempts:
2 left
💡 Hint
Avoid repeating expensive computations in multiple stages.
✗ Incorrect
Computing the field once in $addFields and reusing it avoids redundant calculations and improves performance.
🔧 Debug
advanced2:00remaining
Debug incorrect computed field in pre-aggregation
Why does this pipeline produce null for the computed field
totalCost?MongoDB
db.products.aggregate([
{
$project: {
totalCost: { $multiply: ["$cost", "$quantity"] },
name: 1
}
}
])Attempts:
2 left
💡 Hint
Check if the fields used in $multiply exist in the documents.
✗ Incorrect
If the fields cost or quantity are missing or named differently, $multiply returns null for totalCost.
🧠 Conceptual
expert3:00remaining
Understanding computed pattern for pre-aggregation in MongoDB
Which statement best describes the computed pattern for pre-aggregation in MongoDB?
Attempts:
2 left
💡 Hint
Think about how computed fields help with performance in aggregation pipelines.
✗ Incorrect
The computed pattern pre-calculates fields during aggregation to optimize queries and reduce repeated work.