0
0
MongoDBquery~20 mins

Pipeline mental model (stages flow) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipeline Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB aggregation pipeline?

Consider a collection sales with documents like { item: 'apple', quantity: 5, price: 10 }. What will be the output of this pipeline?

[{ $match: { quantity: { $gt: 3 } } }, { $project: { item: 1, total: { $multiply: ["$quantity", "$price"] } } }]
MongoDB
[{ $match: { quantity: { $gt: 3 } } }, { $project: { item: 1, total: { $multiply: ["$quantity", "$price"] } } }]
A[{ item: 'apple', quantity: 5, price: 10, total: 50 }]
B[{ item: 'apple', total: 50 }]
C[{ item: 'apple', total: 15 }]
D[{ item: 'apple', quantity: 5, price: 10 }]
Attempts:
2 left
💡 Hint

Remember that $project controls which fields appear in the output.

🧠 Conceptual
intermediate
1:00remaining
Which stage in a MongoDB pipeline filters documents?

In the MongoDB aggregation pipeline, which stage is used to filter documents based on a condition?

A$group
B$sort
C$project
D$match
Attempts:
2 left
💡 Hint

Think about which stage selects documents by condition.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this pipeline stage

Which option contains a syntax error in the MongoDB aggregation pipeline stage?

{ $project: { total: { $multiply: ["$quantity", "$price"] } }
A{ $project: { total: { $multiply: "$quantity", "$price" } } }
B} } } ]"ecirp$" ,"ytitnauq$"[ :ylpitlum$ { :latot { :tcejorp$ {
C{ $project: { total: { $multiply: ["$quantity", "$price"] } } }
D $project: { total: { $multiply: ["$quantity", "$price"] } } }
Attempts:
2 left
💡 Hint

Check the syntax of the $multiply operator.

optimization
advanced
2:30remaining
Which pipeline order improves performance by reducing documents early?

You want to optimize a pipeline that calculates total sales and then filters for totals above 100. Which pipeline order is better?

A[{ $project: { total: { $multiply: ["$quantity", "$price"] } } }, { $match: { total: { $gt: 100 } } }]
B[{ $match: { total: { $gt: 100 } } }, { $project: { total: { $multiply: ["$quantity", "$price"] } } }]
C[{ $match: { quantity: { $gt: 0 } } }, { $project: { total: { $multiply: ["$quantity", "$price"] } } }, { $match: { total: { $gt: 100 } } }]
D[{ $project: { total: { $multiply: ["$quantity", "$price"] } } }, { $match: { quantity: { $gt: 0 } } }]
Attempts:
2 left
💡 Hint

Filtering early reduces the number of documents processed in later stages.

🔧 Debug
expert
3:00remaining
Why does this pipeline produce no results?

Given this pipeline:

[{ $match: { total: { $gt: 100 } } }, { $project: { total: { $multiply: ["$quantity", "$price"] } } }]

Why might it return an empty result even if some documents have quantity and price values?

ABecause $match runs before $project, so 'total' does not exist yet
BBecause $project removes all original fields except those specified, so $match cannot see 'total'
CBecause $match cannot filter on fields created in previous $project stages
DBecause $multiply operator is invalid and causes the pipeline to fail silently
Attempts:
2 left
💡 Hint

Think about the order stages run and when fields become available.