MongoDB MCQs – Aggregation Basics

Aggregation in MongoDB is a tool that processes and transforms data using steps like filtering, grouping, and reshaping. It helps calculate sums, averages, and counts and can join collections. Understanding these concepts is important for making your queries efficient and solving complex problems. Interviewers often test your knowledge of these operators and how you apply them, so practicing with multiple-choice questions (MCQs) can be very helpful.

1.) What is the purpose of the MongoDB aggregation pipeline?

A) To perform basic CRUD operations
B) To handle authentication
C) To create indexes
D) To process and transform data in stages

Answer: Option D

Explanation: The aggregation pipeline processes data in multiple stages, enabling data transformation, filtering, and computation efficiently.

2.) Which aggregation stage is used to filter documents based on specified conditions?

A) $group
B) $project
C) $match
D) $sort

Answer: Option C

Explanation: The $match stage filters documents by conditions, similar to the WHERE clause in SQL.

3.) What does the $group stage do in the aggregation pipeline?

A) Filters documents
B) Groups documents by a specified field
C) Projects specific fields
D) Sorts documents

Answer: Option B

Explanation: The $group stage groups documents based on a field and performs aggregation functions like $sum, $avg, etc.

4.) What is the primary purpose of the $project stage in MongoDB?

A) Filters documents
B) Includes or excludes fields in the output
C) Groups documents by a specified field
D) Sorts documents

Answer: Option B

Explanation: The $project stage reshapes documents by specifying the fields to include or exclude in the output.

5.) How do you specify an aggregation pipeline in MongoDB?

A) db.collection.aggregate([ pipeline stages ])
B) db.collection.find([ pipeline stages ])
C) db.collection.query([ pipeline stages ])
D) db.collection.filter([ pipeline stages ])

Answer: Option A

Explanation: The aggregate() method accepts an array of pipeline stages to define the aggregation process.

6.) Which operator in the $group stage is used to count the total number of documents in a group?

A) $count
B) $sum: 1
C) $total
D) $docs

Answer: Option B

Explanation: To count documents in a group, the $sum: 1 operator is used, incrementing the count by 1 for each document in the group.

7.) What does the $count stage in an aggregation pipeline do?

A) Counts the total fields in a document
B) Counts the array elements in a field
C) Counts the unique values in a field
D) Counts the number of documents in the pipeline

Answer: Option D

Explanation: The $count stage returns the number of documents processed in the pipeline.

8.) Which stage is used to sort documents in an aggregation pipeline?

A) $project
B) $group
C) $sort
D) $match

Answer: Option C

Explanation: The $sort stage orders documents based on one or more fields, either in ascending or descending order.

9.) How do you specify a descending sort in the $sort stage?

A) { field: 1 }
B) { field: -1 }
C) { field: “desc” }
D) { field: “descending” }

Answer: Option B

Explanation: { field: -1 } specifies descending order in the $sort stage.

10.) Which operator is used to calculate the average value in a $group stage?

A) $avg
B) $average
C) $ag
D) $groupAvg

Answer: Option A

Explanation: The $avg operator calculates the average value for a group.

Leave a Reply

Your email address will not be published. Required fields are marked *