This query finds students with grades 70 or higher, groups them by class, calculates the average grade per class, and sorts classes by average score descending.
The aggregation pipeline is powerful for data analysis inside MongoDB without moving data outside.
Each stage can do different things like filtering, grouping, sorting, or reshaping data.
Summary
The aggregation pipeline processes data step-by-step inside the database.
It helps with filtering, grouping, sorting, and transforming data easily.
Using it can make data analysis faster and simpler.
Practice
(1/5)
1. Why do we use the aggregation pipeline in MongoDB?
easy
A. To process and transform data step-by-step inside the database
B. To create new databases
C. To backup the database automatically
D. To connect MongoDB with other databases
Solution
Step 1: Understand the purpose of aggregation pipeline
The aggregation pipeline is designed to process data in stages, allowing filtering, grouping, and transforming data inside MongoDB.
Step 2: Compare options with the pipeline's purpose
Creating databases, backups, or connecting databases are not functions of the aggregation pipeline.
Final Answer:
To process and transform data step-by-step inside the database -> Option A
Quick Check:
Aggregation pipeline = step-by-step data processing [OK]
Hint: Aggregation pipeline processes data inside MongoDB stepwise [OK]
Common Mistakes:
Confusing aggregation pipeline with database creation
Thinking it handles backups
Assuming it connects different databases
2. Which of the following is the correct syntax to start an aggregation pipeline on a collection named orders?
easy
A. db.orders.findAggregate([ { $match: { status: 'shipped' } } ])
B. db.orders.find({ $match: { status: 'shipped' } })
C. db.orders.aggregate({ $match: { status: 'shipped' } })
D. db.orders.aggregate([ { $match: { status: 'shipped' } } ])
Solution
Step 1: Identify the correct aggregation method
The aggregation pipeline uses the aggregate() method with an array of stages.
Step 2: Check the syntax of each option
db.orders.aggregate([ { $match: { status: 'shipped' } } ]) correctly uses aggregate with an array containing a $match stage. The other options use invalid methods like findAggregate(), misuse find() with aggregation stages, or pass an object instead of an array to aggregate().