Test your knowledge of MongoDB aggregation with these beginner-friendly questions covering the most common stages and concepts, including $match, $group, $project, $unwind, $lookup, and more. Ideal for interviews and revision on MongoDB’s data processing capabilities.
Understanding Aggregation Framework
What role does the Aggregation Framework play in MongoDB when working with data?
- It is used for manual data entry.
- It only backs up data automatically.
- It deletes duplicated documents by default.
- It processes and transforms data using stages in a pipeline.
Identifying Aggregation Stages
Which of the following is a stage that can be used to filter documents in an aggregation pipeline?
- $match
- $shape
- $insert
- $join
$group Stage Functionality
When using the $group stage in MongoDB aggregation, what is the main result?
- Documents are converted into arrays.
- Documents are permanently deleted.
- Documents are randomly shuffled.
- Documents are grouped by a field and aggregate values like sum or count are calculated.
$project for Shaping Output
How does the $project stage modify documents in a pipeline?
- By duplicating every document.
- By renaming the collection.
- By deleting odd-numbered documents.
- By including, excluding, or modifying specific fields in the output documents.
Deconstructing Arrays with $unwind
If a document contains an items array, what does the $unwind stage do when applied?
- It encrypts the array values.
- It replaces arrays with numeric IDs.
- It merges all arrays into a single field.
- It creates separate documents for each element of the array.
Purpose of $lookup Stage
What is the main purpose of the $lookup stage in a MongoDB aggregation pipeline?
- To perform a join between two collections and combine related data.
- To export data to an external CSV file.
- To change the data types of fields.
- To delete all duplicate documents.
Using $sort in Aggregation
Which of the following describes the function of the $sort stage in an aggregation pipeline?
- It removes any empty arrays.
- It encrypts specific fields.
- It arranges documents in ascending or descending order.
- It splits arrays into separate fields.
Combining $match and $group
What is an advantage of using $match before $group in a pipeline, for example filtering completed orders before aggregating totals?
- It automatically archives all documents.
- It skips the grouping altogether.
- It duplicates documents before grouping.
- It ensures only relevant documents are grouped and aggregated.
Understanding the $bucket Stage
When using the $bucket stage in MongoDB aggregation, what happens to the documents?
- They are encrypted and hidden.
- They are grouped into defined ranges or 'buckets' based on a specific field's value.
- They are bookmarked for later use.
- They are converted into a CSV format.
Using $facet for Multiple Results
What is the primary feature of the $facet stage in MongoDB aggregation?
- It allows multiple aggregation pipelines to run in parallel and returns multiple results.
- It splits documents into different databases.
- It deletes unused fields from every document.
- It compresses the output documents.