Courseiva
Aggregation Framework →hardMultiple Select

C100DEV Aggregation Framework Practice Question

You are reviewing an aggregation pipeline on an `orders` collection that must return only orders with a `total` greater than 100 and then compute the number of orders per `customerId`. The collection has an index on `total`. Which two stages should you use, and in what order, to maximize performance while returning the correct results? (Choose two.)

⚠ Common exam trap

The trap here is thinking that any early stage reduces work, when stages like `$limit` or `$sort` can change results or add cost without using the index for filtering.

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

✓

Place a `$group` stage with `_id: "$customerId"` and `count: { $sum: 1 }` after the filtering stage.

To maximize performance while returning correct counts, the pipeline should start with a `$match` on `total` so the indexed field filters documents early, then use `$group` with `$sum: 1` keyed by `customerId`. This order reduces the number of documents entering the grouping stage and ensures the counts reflect only qualifying orders. Other stages either fail to filter correctly or change the result set.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    Place a `$group` stage with `_id: "$customerId"` and `count: { $sum: 1 }` after the filtering stage.

    Why this is correct

    The `$group` stage with `_id: "$customerId"` and `$sum: 1` computes the number of orders per customer. Placing it after `$match` means it only processes the filtered subset, which is more efficient. This stage produces the required per-customer counts and cannot be replaced by a simple query projection because the aggregation is needed.

  • ✗

    Place a `$project` stage that includes only `customerId` and `total` before the `$group` stage.

    Why it's wrong here

    While a `$project` can reduce document size, adding it before `$group` does not replace the need for `$match` to filter by `total`. Without the `$match`, the pipeline would group all orders, not just those above 100, producing incorrect counts. It also does not leverage the index on `total` for filtering.

  • ✓

    Place a `$match` stage with `{ total: { $gt: 100 } }` as the first stage of the pipeline.

    Why this is correct

    A `$match` as the first stage filters documents before any grouping occurs, reducing the number of documents that reach later stages. Because `total` is indexed, this early filter lets the query planner use an index scan, lowering the number of documents read. This is the standard optimization for aggregation pipelines that begin with a selective filter.

  • ✗

    Place a `$sort` stage on `total` as the first stage to speed up the filter.

    Why it's wrong here

    A `$sort` as the first stage forces the pipeline to sort the entire collection before filtering, which is expensive and does not improve the filter's performance. The index on `total` is already used by `$match` for range queries; sorting first adds overhead without benefit and may hit memory limits for large collections.

  • ✗

    Place a `$limit` stage of 100 before the `$group` stage to reduce the number of documents processed.

    Why it's wrong here

    A `$limit` before `$group` would arbitrarily restrict the input to 100 documents, producing incorrect counts because not all matching orders would be included. The scenario requires counting all orders with `total` greater than 100, so limiting early changes the result set and is not a valid optimization here.

About these practice questions

One of 259 original C100DEV practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.