What is the purpose of the MongoDB aggregation pipeline?
The aggregation pipeline processes data in stages, transforming documents step-by-step to produce aggregated results.
Click to reveal answer
beginner
Name the first stage in a MongoDB aggregation pipeline and its role.
The $match stage filters documents to pass only those that meet specified criteria to the next stage.
Click to reveal answer
intermediate
How does data flow through the stages in a MongoDB pipeline?
Data flows sequentially from one stage to the next, with each stage receiving input from the previous and outputting transformed data.
Click to reveal answer
intermediate
What happens if a stage in the pipeline changes the shape of documents?
The next stage receives documents in the new shape, so each stage can build on transformations made earlier.
Click to reveal answer
intermediate
Why is the order of stages important in a MongoDB aggregation pipeline?
Because each stage depends on the output of the previous one, changing the order can change the final result or cause errors.
Click to reveal answer
What does the $group stage do in a MongoDB pipeline?
AGroups documents by a specified key and aggregates values
BFilters documents based on conditions
CSorts documents in ascending or descending order
DProjects specific fields from documents
✗ Incorrect
The $group stage groups documents by a key and performs aggregation like sum or average.
Which stage should come first to reduce data early in the pipeline?
A$match
B$project
C$group
D$sort
✗ Incorrect
The $match stage filters documents early to reduce the amount of data processed in later stages.
If you want to add a new field to documents, which stage do you use?
A$match
B$addFields
C$group
D$limit
✗ Incorrect
$addFields adds new fields or modifies existing ones in documents.
What happens if you place $sort after $limit in a pipeline?
APipeline will error out
BDocuments are sorted before limiting
COnly limited documents are sorted
DNo effect on output
✗ Incorrect
Placing $sort after $limit sorts only the limited subset of documents.
Which stage reshapes documents to include only specified fields?
A$match
B$group
C$unwind
D$project
✗ Incorrect
$project controls which fields to include or exclude in the output documents.
Explain how data flows through the stages in a MongoDB aggregation pipeline.
Think of it like an assembly line where each step changes the product a bit.
You got /3 concepts.
Why is the order of stages important in a MongoDB pipeline? Give an example.
Consider filtering data early to improve performance.
You got /3 concepts.
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
Step 1: Understand the pipeline concept
Each stage in a pipeline processes data step-by-step, modifying or filtering it.
Step 2: Identify the role of stages
Stages do not create databases or delete collections; they only transform or filter data.
Final Answer:
Transforms or filters data before passing it to the next stage -> Option C
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
Step 1: Identify aggregation pipeline syntax
The aggregation pipeline uses the aggregate() method with an array of stages.
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.
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
Step 1: Analyze the $match stage
This stage filters documents where age is 18 or more.
Step 2: Analyze the $group stage
Groups filtered documents by city and counts how many per city.
Final Answer:
Count of people grouped by city who are 18 or older -> Option A
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
Step 1: Check multiple $match stages
MongoDB allows multiple $match stages anywhere in the pipeline.
Step 2: Check $project usage
$project can select fields; _id is included by default unless excluded explicitly.
Final Answer:
The pipeline syntax is correct and will work as expected -> Option B
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?