C100DBA practice question 18 of 25

Which of the following command inside aggregate command is used in MongoDB aggregation to filter the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

Full set: 130 questions
Answer options
A: $aggregate
B: $sum
C: $match Correct answer
D: $group
Correct Answer:
C. $match
Explanation

In the MongoDB aggregation framework, the pipeline consists of stages. The $match stage is specifically designed to filter documents. It processes all documents in a collection and outputs only those that satisfy the specified query conditions. This stage is typically placed early in the pipeline to reduce the amount of data processed by subsequent stages, which improves performance. It uses standard MongoDB query syntax.

Why Incorrect

A. $aggregate is the command that initiates the aggregation pipeline; it is not a filtering stage within the pipeline itself.
B. $sum is an accumulator operator used within the $group stage to calculate a sum, not for filtering documents.
D. $group is a pipeline stage that groups input documents by a specified identifier expression and applies accumulator expressions to each group.

References

1. MongoDB Manual, Aggregation Pipeline Stages, $match. (https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/) "Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage."

2. MongoDB University, M121: The MongoDB Aggregation Framework, Chapter 2: Basic Aggregation - $match and $project. "The $match stage provides a query mechanism to filter documents in an aggregation pipeline."

Scroll to Top