0
0
MongoDBquery~20 mins

How the engine optimizes pipelines in MongoDB - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipeline Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Pipeline Stage Reordering Effect
Consider a MongoDB aggregation pipeline on a collection with 1 million documents. The pipeline is:
[{ $project: { name: 1, age: 1 } }, { $match: { age: { $gt: 30 } } }]

What is the main effect of reordering the stages to put $match before $project?
MongoDB
[{ $project: { name: 1, age: 1 } }, { $match: { age: { $gt: 30 } } }]
AThere is no difference in performance because MongoDB executes stages in parallel.
BThe pipeline will run slower because projecting first reduces fields needed for matching.
CThe pipeline will run faster because filtering happens before projecting, reducing data early.
DThe pipeline will fail because $match cannot come after $project.
Attempts:
2 left
💡 Hint
Think about filtering data early to reduce workload.
🧠 Conceptual
intermediate
1:30remaining
Understanding Pipeline Optimization
Which of the following best describes how MongoDB optimizes aggregation pipelines internally?
AIt automatically reorders stages to filter and reduce data as early as possible.
BIt executes all stages in the order written without any changes.
CIt converts all stages into a single map-reduce operation.
DIt duplicates data to speed up parallel processing of stages.
Attempts:
2 left
💡 Hint
Think about how to reduce data early in a pipeline.
📝 Syntax
advanced
2:00remaining
Identify the Invalid Pipeline Stage
Which of the following pipeline stages will cause a syntax error when used in a MongoDB aggregation pipeline?
A{ $filter: { input: "$items", as: "item", cond: { $gt: ["$$item.price", 100] } } }
B{ $group: { _id: "$city", total: { $sum: 1 } } }
C{ $match: { age: { $gte: 18 } } }
D{ $sort: { name: 1 } }
Attempts:
2 left
💡 Hint
Check if the stage is a valid aggregation pipeline stage operator.
optimization
advanced
2:30remaining
Optimizing Pipeline with Indexes
Given a collection with an index on the field status, which pipeline will best utilize the index to optimize performance?
A[{ $sort: { status: 1 } }, { $match: { status: "active" } }]
B[{ $group: { _id: "$category", count: { $sum: 1 } } }, { $match: { status: "active" } }]
C[{ $project: { status: 1, category: 1 } }, { $match: { status: "active" } }]
D[{ $match: { status: "active" } }, { $group: { _id: "$category", count: { $sum: 1 } } }]
Attempts:
2 left
💡 Hint
Indexes help when filtering happens early.
🔧 Debug
expert
3:00remaining
Diagnose Pipeline Performance Issue
A developer notices their aggregation pipeline is slow. The pipeline is:
[{ $project: { name: 1, age: 1, status: 1 } }, { $match: { status: "active" } }, { $group: { _id: "$age", count: { $sum: 1 } } }]

What is the main reason for the slow performance?
AThe $group stage is missing an index, causing slow grouping.
BThe $match stage comes after $project, so filtering happens late, processing more data than needed.
CThe $project stage removes the status field needed for $match, causing an error.
DThe pipeline uses $sum incorrectly, causing a runtime error.
Attempts:
2 left
💡 Hint
Consider the order of filtering and projecting.