0
0
MongoDBquery~20 mins

Why expressions matter in pipelines in MongoDB - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipeline Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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"] } } }])
A[{ "totalCost": null }]
B[{ "totalCost": 12 }]
C[{ "totalCost": 10 }]
D[{ "totalCost": 20 }]
Attempts:
2 left
💡 Hint
Think about how $multiply works with the fields price and quantity.
🧠 Conceptual
intermediate
1:30remaining
Why use expressions in MongoDB pipelines?
Which of the following best explains why expressions are important in MongoDB aggregation pipelines?
AThey allow transforming and computing new fields dynamically during data processing.
BThey automatically index the collection for faster queries.
CThey replace the need for a database schema.
DThey encrypt data during aggregation.
Attempts:
2 left
💡 Hint
Think about what expressions do inside a pipeline stage.
📝 Syntax
advanced
2: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" } } } }])
ATypeError: Cannot add string and number
BSyntaxError: Missing closing bracket in $add array
CRuntimeError: Field price does not exist
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the brackets around the $add array.
optimization
advanced
2: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?
A{ $project: { discountPrice: { $multiply: ["$price", 0.9] } } }
B{ $project: { discountPrice: { $subtract: ["$price", { $multiply: ["$price", 0.1] }] } } }
C{ $project: { discountPrice: { $add: ["$price", -0.1] } } }
D{ $project: { discountPrice: { $divide: ["$price", 1.1] } } }
Attempts:
2 left
💡 Hint
Consider the number of operations and clarity.
🔧 Debug
expert
3:00remaining
Why does this pipeline produce unexpected results?
Consider this pipeline:
[{ $project: { total: { $add: ["$price", "$quantity"] } } }, { $match: { total: { $gt: 10 } } }]

Why might the $match stage not filter documents as expected?
ABecause $gt operator cannot be used inside $match.
BBecause $add cannot add two fields together.
CBecause $project creates a new field total but does not persist it for $match to use.
DBecause $match must come before $project in the pipeline.
Attempts:
2 left
💡 Hint
Think about how fields created in $project are available to later stages.