Bird
Raised Fist0
MongoDBquery~20 mins

Pipeline execution order matters in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 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 aggregation pipeline?

Consider a MongoDB collection sales with documents like:

{ "item": "apple", "price": 10, "quantity": 5 }

What will be the output of this pipeline?

db.sales.aggregate([
  { $match: { price: { $gt: 5 } } },
  { $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } }
])
MongoDB
db.sales.aggregate([
  { $match: { price: { $gt: 5 } } },
  { $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } }
])
A[]
B[ { _id: "apple", totalQuantity: 0 } ]
C[ { _id: null, totalQuantity: 5 } ]
D[ { _id: "apple", totalQuantity: 5 } ]
Attempts:
2 left
💡 Hint

Remember that $match filters documents before grouping.

❓ query_result
intermediate
2:00remaining
How does changing the order of $group and $match affect output?

Given the same sales collection, what is the output of this pipeline?

db.sales.aggregate([
  { $group: { _id: "$item", totalQuantity: { $sum: "$quantity" }, avgPrice: { $avg: "$price" } } },
  { $match: { avgPrice: { $gt: 5 } } }
])
MongoDB
db.sales.aggregate([
  { $group: { _id: "$item", totalQuantity: { $sum: "$quantity" }, avgPrice: { $avg: "$price" } } },
  { $match: { avgPrice: { $gt: 5 } } }
])
A[ { _id: "apple", totalQuantity: 5 } ]
B[ { _id: "apple", totalQuantity: 5, avgPrice: 10 } ]
C[]
D[ { _id: null, totalQuantity: 5, avgPrice: 10 } ]
Attempts:
2 left
💡 Hint

Think about when filtering happens: before or after grouping.

🧠 Conceptual
advanced
2:00remaining
Which pipeline is inefficient due to stage order?

Which of these MongoDB aggregation pipelines is inefficient because it processes more data than necessary?

Adb.collection.aggregate([{ $sort: { price: 1 } }, { $match: { price: { $gt: 10 } } }])
Bdb.collection.aggregate([{ $match: { price: { $gt: 10 } } }, { $sort: { price: 1 } }])
Cdb.collection.aggregate([{ $group: { _id: "$category", total: { $sum: "$amount" } } }, { $sort: { total: -1 } }])
Ddb.collection.aggregate([{ $project: { price: 1, category: 1, amount: 1 } }, { $group: { _id: "$category", total: { $sum: "$amount" } } }])
Attempts:
2 left
💡 Hint

Place $match before $sort to reduce the dataset early.

🔧 Debug
advanced
2:00remaining
Why does this pipeline return no results?

Given this pipeline:

db.orders.aggregate([
  { $group: { _id: "$customer", totalSpent: { $sum: "$amount" } } },
  { $match: { amount: { $gt: 100 } } }
])

Why does it return no results?

MongoDB
db.orders.aggregate([
  { $group: { _id: "$customer", totalSpent: { $sum: "$amount" } } },
  { $match: { amount: { $gt: 100 } } }
])
ABecause $match must come before $group
BBecause $group removes all documents
CBecause $match filters on a field 'amount' that does not exist after $group
DBecause $sum operator is used incorrectly
Attempts:
2 left
💡 Hint

Check the fields available after the $group stage.

🧠 Conceptual
expert
2:00remaining
Why does pipeline order affect performance and results?

Which statement best explains why the order of stages in a MongoDB aggregation pipeline matters?

ABecause stages are executed in sequence, changing order can change which documents are processed and how much data flows through the pipeline
BBecause MongoDB automatically reorders stages for best performance, order does not matter
CBecause only the first stage affects the output, later stages are ignored
DBecause the order only affects the output format, not the data processed
Attempts:
2 left
💡 Hint

Think about filtering early vs late in the pipeline.

Practice

(1/5)
1. In MongoDB aggregation pipelines, why does the order of stages matter?
easy
A. Because stages run in the order they are written, affecting results and performance
B. Because MongoDB runs all stages in parallel regardless of order
C. Because only the last stage affects the output
D. Because stages can be reordered automatically by MongoDB

Solution

  1. Step 1: Understand pipeline execution

    MongoDB executes pipeline stages sequentially in the order they appear.
  2. Step 2: Impact of order on results and performance

    Changing the order can change the output and how fast the query runs, especially filtering early helps performance.
  3. Final Answer:

    Because stages run in the order they are written, affecting results and performance -> Option A
  4. Quick Check:

    Pipeline order affects output and speed [OK]
Hint: Remember: pipeline stages run top to bottom [OK]
Common Mistakes:
  • Thinking MongoDB runs stages in parallel
  • Believing order does not affect output
  • Assuming MongoDB reorders stages automatically
2. Which of the following is the correct syntax to start a MongoDB aggregation pipeline with a filter stage?
easy
A. db.collection.aggregate([{ $match: { status: 'A' } }])
B. db.collection.aggregate({ $match: { status: 'A' } })
C. db.collection.find({ $match: { status: 'A' } })
D. db.collection.aggregate([{ match: { status: 'A' } }])

Solution

  1. Step 1: Identify correct aggregation syntax

    The aggregate method takes an array of stages, each stage is an object with a stage operator like $match.
  2. Step 2: Check each option

    db.collection.aggregate([{ $match: { status: 'A' } }]) correctly uses an array with $match stage. db.collection.aggregate({ $match: { status: 'A' } }) misses array brackets. db.collection.find({ $match: { status: 'A' } }) uses find() which is not aggregation. db.collection.aggregate([{ match: { status: 'A' } }]) misses $ in $match.
  3. Final Answer:

    db.collection.aggregate([{ $match: { status: 'A' } }]) -> Option A
  4. Quick Check:

    Aggregation stages are in an array [OK]
Hint: Aggregation stages must be in an array [OK]
Common Mistakes:
  • Omitting the array brackets around stages
  • Using find() instead of aggregate() for pipelines
  • Missing $ in stage operators like $match
3. Given this pipeline:
db.orders.aggregate([
  { $sort: { price: -1 } },
  { $match: { status: 'A' } }
])

What is the main issue with this pipeline?
medium
A. It sorts ascending instead of descending
B. It will cause a syntax error due to stage order
C. It will return no results because $match must come first
D. It filters after sorting, which is less efficient

Solution

  1. Step 1: Analyze stage order

    The pipeline sorts all documents first, then filters with $match.
  2. Step 2: Understand performance impact

    Filtering first reduces documents early, making sorting faster. Sorting before filtering wastes resources.
  3. Final Answer:

    It filters after sorting, which is less efficient -> Option D
  4. Quick Check:

    Filter early, then sort for best performance [OK]
Hint: Filter before sort to improve speed [OK]
Common Mistakes:
  • Thinking stage order causes syntax errors
  • Believing $match must always be first to work
  • Confusing sort order direction
4. You wrote this pipeline but it returns an error:
db.sales.aggregate([
  { $group: { _id: "$item", total: { $sum: "$amount" } } },
  { $match: { $gt: ["$total", 100] } }
])

What is the likely cause?
medium
A. The $match stage cannot filter on fields created by $group
B. The $match stage should use $expr to filter on computed fields
C. The $match stage must come before $group to filter correctly
D. The $group stage syntax is incorrect

Solution

  1. Step 1: Identify the invalid $match syntax

    The $match stage uses aggregation expression syntax { $gt: ["$total", 100] } directly, which is not valid query selector syntax.
  2. Step 2: Use $expr for aggregation expressions

    To use expression operators like $gt with field paths and literals in array form, wrap in $expr: { $match: { $expr: { $gt: ["$total", 100] } } }.
  3. Final Answer:

    The $match stage should use $expr to filter on computed fields -> Option B
  4. Quick Check:

    $expr enables agg expressions in $match [OK]
Hint: Use $expr in $match after $group [OK]
Common Mistakes:
  • Trying to filter grouped fields without $expr
  • Placing $match before $group expecting same results
  • Assuming $group syntax is wrong
5. You want to find the top 3 most expensive products with status 'A'. Which pipeline correctly applies the stages for best performance?
hard
A. db.products.aggregate([ { $sort: { price: -1 } }, { $limit: 3 }, { $match: { status: 'A' } } ])
B. db.products.aggregate([ { $limit: 3 }, { $match: { status: 'A' } }, { $sort: { price: -1 } } ])
C. db.products.aggregate([ { $match: { status: 'A' } }, { $sort: { price: -1 } }, { $limit: 3 } ])
D. db.products.aggregate([ { $match: { status: 'A' } }, { $limit: 3 }, { $sort: { price: -1 } } ])

Solution

  1. Step 1: Filter early for performance

    Applying $match first reduces documents to only those with status 'A'.
  2. Step 2: Sort and limit after filtering

    Sorting after filtering is faster, then $limit picks top 3.
  3. Step 3: Check other options

    db.products.aggregate([ { $sort: { price: -1 } }, { $limit: 3 }, { $match: { status: 'A' } } ]) sorts all documents before filtering, wasting resources. db.products.aggregate([ { $limit: 3 }, { $match: { status: 'A' } }, { $sort: { price: -1 } } ]) limits before filtering, wrong results. db.products.aggregate([ { $match: { status: 'A' } }, { $limit: 3 }, { $sort: { price: -1 } } ]) limits before sorting, wrong order.
  4. Final Answer:

    db.products.aggregate([ { $match: { status: 'A' } }, { $sort: { price: -1 } }, { $limit: 3 } ]) -> Option C
  5. Quick Check:

    Filter, then sort, then limit [OK]
Hint: Filter first, then sort, then limit [OK]
Common Mistakes:
  • Sorting before filtering wastes resources
  • Limiting before sorting gives wrong top results
  • Filtering after limiting misses correct documents
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard