0
0
MongoDBquery~20 mins

Pipeline execution order matters 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 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.