MongoDB MCQs – Aggregation Basics

11.) What does the $addFields stage do in MongoDB?

A) Adds a new field to the document
B) Adds a field to the $match stage
C) Combines two fields into one
D) None

Answer: Option A

Explanation: The $addFields stage adds new fields to documents, optionally using expressions to compute their values.

12.) Which stage limits the number of documents in an aggregation pipeline?

A) $skip
B) $limit
C) $lm
D) $match

Answer: Option B

Explanation: The $limit stage restricts the number of documents passed to the next stage in the pipeline.

13.) What does the $unwind stage do in an aggregation pipeline?

A) Combines multiple fields into an array
B) Removes duplicate documents
C) Filters documents based on an array
D) Breaks an array field into multiple documents

Answer: Option D

Explanation: The $unwind stage deconstructs an array field so that each element is represented as a separate document.

14.) Which stage is used to skip a specified number of documents in the pipeline?

A) $limit
B) $jump
C) $skip
D) $match

Answer: Option C

Explanation: The $skip stage skips the specified number of documents before passing them to the next stage.

15.) Which operator is used in $project to rename a field?

A) $rename
B) : 1
C) $set
D) Alias

Answer: Option D

Explanation: In $project, you can rename a field by specifying its alias in the output.

16.) What is the correct order of stages in the aggregation pipeline?

A) $match → $group → $project
B) $project → $match → $group
C) $group → $project → $match
D) Any order

Answer: Option A

Explanation: Filtering should generally occur early ($match), followed by grouping ($group), and reshaping the data ($project).

17.) Which stage in the aggregation pipeline is used to join data from another collection?

A) $join
B) $lookup
C) $match
D) $merge

Answer: Option B

Explanation: The $lookup stage performs a left outer join to retrieve data from another collection.

18.) Which aggregation stage is used to combine multiple documents into a single output document with cumulative calculations?

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

Answer: Option C

Explanation: The $group stage is used to combine multiple documents into groups and perform cumulative calculations like $sum, $avg, $max, and $min.

19.) What is the $expr operator used for in an aggregation pipeline?

A) To add expressions for sorting
B) To filter documents using complex conditions
C) To count fields in a document
D) To combine multiple arrays

Answer: Option B

Explanation: $expr allows the use of aggregation expressions within a $match stage.

20.) Which stage combines documents from multiple pipelines into a single result?

A) $unionWith
B) $merge
C) $lookup
D) $group

Answer: Option A

Explanation: The $unionWith stage combines documents from the current pipeline with those from another collection or pipeline.

Leave a Reply

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