Bird
Raised Fist0
MongoDBquery~20 mins

Pipeline mental model (stages flow) 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 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.

Practice

(1/5)
1. What does each stage in a MongoDB aggregation pipeline do?
easy
A. Runs a SQL query on the data
B. Creates a new database
C. Transforms or filters data before passing it to the next stage
D. Deletes the entire collection

Solution

  1. Step 1: Understand the pipeline concept

    Each stage in a pipeline processes data step-by-step, modifying or filtering it.
  2. Step 2: Identify the role of stages

    Stages do not create databases or delete collections; they only transform or filter data.
  3. Final Answer:

    Transforms or filters data before passing it to the next stage -> Option C
  4. Quick Check:

    Pipeline stages transform or filter data [OK]
Hint: Remember: pipeline stages change data step-by-step [OK]
Common Mistakes:
  • Thinking stages create or delete collections
  • Confusing pipeline with database commands
  • Assuming stages run SQL queries
2. Which of the following is the correct syntax to start an aggregation pipeline in MongoDB?
easy
A. db.collection.find({ $match: { status: 'A' } })
B. db.collection.update({ $match: { status: 'A' } })
C. db.collection.insert([{ $match: { status: 'A' } }])
D. db.collection.aggregate([ { $match: { status: 'A' } } ])

Solution

  1. Step 1: Identify aggregation pipeline syntax

    The aggregation pipeline uses the aggregate() method with an array of stages.
  2. Step 2: Compare options

    Only db.collection.aggregate([ { $match: { status: 'A' } } ]) uses aggregate() with a pipeline stage ($match). Others use find, insert, or update incorrectly.
  3. Final Answer:

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

    aggregate() starts pipelines [OK]
Hint: aggregate() method always starts a pipeline [OK]
Common Mistakes:
  • Using find() instead of aggregate() for pipelines
  • Trying to insert pipeline stages
  • Using update() with pipeline syntax
3. Given the pipeline: [{ $match: { age: { $gte: 18 } } }, { $group: { _id: '$city', count: { $sum: 1 } } }], what does the output represent?
medium
A. Count of people grouped by city who are 18 or older
B. List of all people younger than 18
C. Sum of ages for each city
D. All documents without filtering

Solution

  1. Step 1: Analyze the $match stage

    This stage filters documents where age is 18 or more.
  2. Step 2: Analyze the $group stage

    Groups filtered documents by city and counts how many per city.
  3. Final Answer:

    Count of people grouped by city who are 18 or older -> Option A
  4. Quick Check:

    $match filters, $group counts by city [OK]
Hint: $match filters, $group aggregates data [OK]
Common Mistakes:
  • Confusing $sum with sum of ages
  • Ignoring the filter condition
  • Assuming output lists all people
4. What is wrong with this pipeline? [{ $match: { score: { $gt: 50 } } }, { $project: { score: 1, name: 1 } }, { $match: { name: 'Alice' } }]
medium
A. Using two $match stages is not allowed
B. The pipeline syntax is correct and will work as expected
C. The $project stage must include _id field explicitly
D. The second $match stage should come before $project

Solution

  1. Step 1: Check multiple $match stages

    MongoDB allows multiple $match stages anywhere in the pipeline.
  2. Step 2: Check $project usage

    $project can select fields; _id is included by default unless excluded explicitly.
  3. Final Answer:

    The pipeline syntax is correct and will work as expected -> Option B
  4. Quick Check:

    Multiple $match and $project usage is valid [OK]
Hint: Multiple $match stages are allowed anywhere [OK]
Common Mistakes:
  • Thinking multiple $match stages are invalid
  • Assuming _id must be included explicitly
  • Believing $project must come last
5. You want to find the average price of products in each category but only for products with stock greater than 0. Which pipeline correctly achieves this?
hard
A. [{ $match: { stock: { $gt: 0 } } }, { $group: { _id: '$category', avgPrice: { $avg: '$price' } } }]
B. [{ $group: { _id: '$category', avgPrice: { $avg: '$price' } } }, { $match: { stock: { $gt: 0 } } }]
C. [{ $project: { category: 1, price: 1, stock: 1 } }, { $group: { _id: '$category', avgPrice: { $avg: '$price' } } }]
D. [{ $match: { price: { $gt: 0 } } }, { $group: { _id: '$category', avgPrice: { $avg: '$stock' } } }]

Solution

  1. Step 1: Filter products with stock > 0 first

    The $match stage must come first to filter only products in stock.
  2. Step 2: Group by category and calculate average price

    Use $group with _id as category and $avg on price to get average price per category.
  3. Final Answer:

    [{ $match: { stock: { $gt: 0 } } }, { $group: { _id: '$category', avgPrice: { $avg: '$price' } } }] -> Option A
  4. Quick Check:

    Filter then group for correct average [OK]
Hint: Filter first, then group to aggregate correctly [OK]
Common Mistakes:
  • Grouping before filtering causes wrong results
  • Averaging wrong fields
  • Filtering on price instead of stock
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