0
0
MongoDBquery~20 mins

Computed pattern for pre-aggregation in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Pre-Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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
    }
  }
])
A[{ "item": "apple", "totalSale": null }, { "item": "banana", "totalSale": null }]
B[{ "item": "apple", "totalSale": 3 }, { "item": "banana", "totalSale": 2 }]
C[{ "item": "apple", "totalSale": 30 }, { "item": "banana", "totalSale": 20 }]
D[{ "item": "apple", "totalSale": "30" }, { "item": "banana", "totalSale": "20" }]
Attempts:
2 left
💡 Hint
Remember that $multiply computes the product of numeric fields.
📝 Syntax
intermediate
1: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
    }
  }
])
ANo syntax error; the pipeline is correct
BMissing comma between fields inside $project
CField names must not be quoted inside $add
DUsing $add with an array is invalid syntax
Attempts:
2 left
💡 Hint
Check the use of $add operator and field quoting.
optimization
advanced
2: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?
AUse $group to compute the field multiple times
BCompute the field once in a $addFields stage and reuse it later
CRepeat the computation in every $project stage where needed
DStore the computed field in the original documents manually
Attempts:
2 left
💡 Hint
Avoid repeating expensive computations in multiple stages.
🔧 Debug
advanced
2: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
    }
  }
])
AField names cost and quantity do not exist in documents
BUsing $multiply with strings causes null output
CMissing $match stage before $project
DIncorrect use of $project operator
Attempts:
2 left
💡 Hint
Check if the fields used in $multiply exist in the documents.
🧠 Conceptual
expert
3:00remaining
Understanding computed pattern for pre-aggregation in MongoDB
Which statement best describes the computed pattern for pre-aggregation in MongoDB?
AIt only works with $group stages and cannot use $project or $addFields
BIt requires storing all computed fields permanently in the database documents
CIt uses map-reduce exclusively to compute aggregated values
DIt involves computing derived fields during aggregation to avoid repeated calculations later
Attempts:
2 left
💡 Hint
Think about how computed fields help with performance in aggregation pipelines.