MongoDB’s Aggregation Framework is a powerful tool for data transformation and analysis. It enables efficient processing of data through the aggregation pipeline, consisting of stages like $match, $group, $project, and $lookup for joins. This article dives into aggregation basics, common stages, and real-world use cases to harness MongoDB’s full potential.
Aggregation Pipeline Basics
What is the purpose of the Aggregation Framework in MongoDB? a) Perform data analysis and transformation b) Create database schemas c) Handle user authentication d) Automate backups
Which of the following represents an aggregation pipeline in MongoDB? a) A collection of indexed fields b) A sequence of data processing stages c) A type of database schema d) A clustering method
Which command initiates an aggregation pipeline? a) db.aggregate() b) db.collection.aggregate() c) db.createAggregation() d) db.query.aggregate()
What is the output format of an aggregation pipeline? a) Binary data b) JSON-like documents c) Plain text d) CSV
Which stage in the aggregation pipeline is commonly used to filter documents? a) $group b) $project c) $match d) $lookup
Common Stages: $match, $group, $project
What is the main purpose of the $match stage? a) Modify document structure b) Filter documents based on conditions c) Perform joins across collections d) Calculate aggregate values
How does the $group stage in MongoDB operate? a) Filters documents b) Projects fields to specific formats c) Groups documents by specified keys d) Sorts documents in descending order
Which operator is often used in $group for summing values? a) $concat b) $avg c) $sum d) $min
What does the $project stage allow in MongoDB aggregation? a) Project documents into separate collections b) Rename, add, or remove fields c) Apply sorting to documents d) Perform joins across collections
Which of the following is NOT supported in the $match stage? a) Regular expressions b) Arithmetic expressions c) Range queries d) Logical operators
Working with $lookup (Joins in MongoDB)
What is the primary purpose of the $lookup stage? a) Create indexes on documents b) Perform join operations between collections c) Sort documents alphabetically d) Filter documents by conditions
Which field in $lookup defines the local key to match documents? a) foreignField b) as c) localField d) match
What is the result of a $lookup operation in MongoDB? a) A new collection b) An embedded array of matching documents c) A CSV file d) A single merged document
In which scenario is $lookup most useful? a) Filtering by specific conditions b) Joining data from two collections c) Aggregating numeric data d) Sorting documents
What happens if no matches are found in $lookup? a) The pipeline throws an error b) The document is excluded from the output c) An empty array is returned in the joined field d) The aggregation pipeline stops execution
Use Cases for Data Transformation
Which stage is used for changing the document structure? a) $sort b) $project c) $count d) $limit
What is a common use case for the $group stage? a) Sorting documents by date b) Calculating total sales by product c) Joining collections d) Filtering documents
Which stage would you use to rename fields in documents? a) $set b) $unwind c) $project d) $limit
What stage allows breaking an array into multiple documents? a) $project b) $unwind c) $group d) $lookup
What does the $sort stage do? a) Removes duplicate documents b) Organizes documents in a specified order c) Joins two collections d) Filters documents
Mixed Questions
Which stage aggregates numerical data like averages or sums? a) $group b) $match c) $lookup d) $sort
What does the $addFields stage do? a) Adds indexes to fields b) Adds or modifies fields in the documents c) Filters documents by conditions d) Sorts fields alphabetically
Which stage is used to limit the number of documents? a) $limit b) $group c) $match d) $project
Which aggregation stage is used for counting documents? a) $count b) $group c) $lookup d) $project
Which stage combines multiple pipelines? a) $merge b) $facet c) $lookup d) $match
What is the $replaceRoot stage used for? a) Replacing fields with null b) Replacing the root document with a specified sub-document c) Adding new fields to documents d) Aggregating numerical values
Which stage is commonly used for pagination? a) $skip b) $unwind c) $merge d) $group
What does the $bucket stage do? a) Groups documents into buckets based on a defined range b) Sorts documents by a numeric value c) Joins collections using a key d) Adds new fields to documents
What is a common use case for the $merge stage? a) Sorting documents by name b) Writing pipeline results to a collection c) Filtering out unwanted documents d) Counting total documents
Which stage flattens nested arrays into documents? a) $lookup b) $unwind c) $group d) $sort
Answer Key
Qno
Answer
1
a) Perform data analysis and transformation
2
b) A sequence of data processing stages
3
b) db.collection.aggregate()
4
b) JSON-like documents
5
c) $match
6
b) Filter documents based on conditions
7
c) Groups documents by specified keys
8
c) $sum
9
b) Rename, add, or remove fields
10
b) Arithmetic expressions
11
b) Perform join operations between collections
12
c) localField
13
b) An embedded array of matching documents
14
b) Joining data from two collections
15
c) An empty array is returned in the joined field
16
b) $project
17
b) Calculating total sales by product
18
c) $project
19
b) $unwind
20
b) Organizes documents in a specified order
21
a) $group
22
b) Adds or modifies fields in the documents
23
a) $limit
24
a) $count
25
b) $facet
26
b) Replacing the root document with a specified sub-document
27
a) $skip
28
a) Groups documents into buckets based on a defined range