C100DEV Aggregation Framework Practice Question
You are building an aggregation pipeline that processes millions of documents. The pipeline includes a $match stage, a $group stage that calculates sums, and a $sort stage. You notice the pipeline is spilling to disk and running slowly. Which stage should you place first to optimize performance while maintaining correct results?
⚠ Common exam trap
The trap here is thinking that $project or $sort first might help, but they don't reduce document count or leverage indexes as effectively as $match.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
$match
The $match stage should be placed at the beginning of the pipeline to filter documents early, using indexes when possible. This reduces the volume of data passed to $group and $sort, lowering memory consumption and avoiding disk spills. It also maintains the correct aggregation logic by filtering before grouping and sorting.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
$group
Why it's wrong here
Placing $group first would process all documents before any filtering, causing unnecessary computation and memory usage. It would also change the results if the $match was intended to filter before grouping, as it would group unfiltered data. This is inefficient and semantically incorrect.
- ✗
$sort
Why it's wrong here
Placing $sort first would sort the entire collection before filtering, which is extremely resource-intensive and unnecessary. It would not benefit from indexes on the match fields and could cause the pipeline to exceed memory limits. Sorting should typically occur after reducing the dataset.
- ✓
$match
Why this is correct
Placing $match first reduces the number of documents flowing into subsequent stages. This leverages indexes, minimizes memory usage in $group and $sort, and prevents disk spills. It is the standard optimization for aggregation pipelines, ensuring only relevant data is processed.
- ✗
$project
Why it's wrong here
While $project can reduce document size, placing it first does not filter documents. It might help slightly by passing fewer fields, but it does not reduce the number of documents and thus does not address the core issue of processing millions of records. $match is more effective.
About these practice questions
This C100DEV question is part of Courseiva's 259-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official MongoDB exam blueprint
This C100DEV practice question is part of Courseiva's free MongoDB certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the C100DEV exam.