Challenge - 5 Problems
Pipeline Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of $project with expression in MongoDB pipeline
Given the collection
sales with documents like { item: "apple", price: 10, quantity: 2 }, what is the output of this aggregation pipeline?[{ $project: { totalCost: { $multiply: ["$price", "$quantity"] } } }]MongoDB
db.sales.aggregate([{ $project: { totalCost: { $multiply: ["$price", "$quantity"] } } }])Attempts:
2 left
💡 Hint
Think about how $multiply works with the fields price and quantity.
✗ Incorrect
The $multiply expression multiplies the values of price (10) and quantity (2), resulting in 20.
🧠 Conceptual
intermediate1:30remaining
Why use expressions in MongoDB pipelines?
Which of the following best explains why expressions are important in MongoDB aggregation pipelines?
Attempts:
2 left
💡 Hint
Think about what expressions do inside a pipeline stage.
✗ Incorrect
Expressions let you compute new values or transform existing fields as data flows through the pipeline.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this pipeline expression
What error does this MongoDB aggregation pipeline produce?
[{ $project: { total: { $add: ["$price", "$quantity" } } } }]MongoDB
db.collection.aggregate([{ $project: { total: { $add: ["$price", "$quantity" } } } }])Attempts:
2 left
💡 Hint
Check the brackets around the $add array.
✗ Incorrect
The $add operator expects an array, but the closing bracket is missing, causing a syntax error.
❓ optimization
advanced2:30remaining
Optimizing pipeline expressions for performance
Which pipeline expression is more efficient when calculating a discount price as 90% of the original price in MongoDB?
Attempts:
2 left
💡 Hint
Consider the number of operations and clarity.
✗ Incorrect
Multiplying price by 0.9 is a single, clear operation and more efficient than subtracting a computed value or dividing.
🔧 Debug
expert3:00remaining
Why does this pipeline produce unexpected results?
Consider this pipeline:
Why might the $match stage not filter documents as expected?
[{ $project: { total: { $add: ["$price", "$quantity"] } } }, { $match: { total: { $gt: 10 } } }]Why might the $match stage not filter documents as expected?
Attempts:
2 left
💡 Hint
Think about how fields created in $project are available to later stages.
✗ Incorrect
Fields created in $project are available to subsequent stages, but if the pipeline is run incorrectly or the field is missing, $match may not filter as expected. However, the main issue is that $project replaces the document fields, so if total is the only field projected, $match can use it. If other fields are missing, it might cause confusion.