Courseiva
Aggregation Framework →mediumMultiple Choice

C100DEV Aggregation Framework Practice Question

An application requires grouping orders by customer ID and calculating the total purchase amount per customer. However, some customers have orders with missing or null purchase amounts that must be excluded before calculation. Which aggregation pipeline stage sequence correctly achieves this goal?

⚠ Common exam trap

Candidates frequently place the filtering condition inside the $group stage using conditional expressions, forgetting that $match early in the pipeline provides critical index optimization benefits.

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: { purchaseAmount: { $ne: null } } }, { $group: { _id: "$customerId", total: { $sum: "$purchaseAmount" } } }

Filtering out documents with null or missing purchase amounts using the $match stage ensures that subsequent grouping operations do not process corrupted or incomplete data. Placing $match before $group also allows MongoDB to utilize indexes on the purchaseAmount field, significantly improving aggregation performance on large collections.

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: { _id: "$customerId", total: { $sum: "$purchaseAmount" } } }, { $match: { purchaseAmount: { $ne: null } } }

    Why it's wrong here

    Filtering after the $group stage is inefficient because documents with null purchase amounts have already been processed and aggregated into the group totals, corrupting the final calculations and preventing index usage during the initial scan.

  • ✗

    { $project: { customerId: 1, purchaseAmount: 1 } }, { $group: { _id: "$customerId", total: { $sum: "$purchaseAmount" } } }

    Why it's wrong here

    $project with inclusion flags only reshapes documents; it neither drops orders lacking purchaseAmount nor filters nulls, so $sum still counts those customers. Projection is for selecting or computing fields, and would be correct when the pipeline needs a reduced document shape before grouping, not removal of invalid amounts.

  • ✓

    { $match: { purchaseAmount: { $ne: null } } }, { $group: { _id: "$customerId", total: { $sum: "$purchaseAmount" } } }

    Why this is correct

    Filtering with `$match` before `$group` removes documents whose `purchaseAmount` is null, satisfying the exclusion constraint, so `$sum` only accumulates valid numeric values per `customerId`. Running `$group` first would still sum nulls as zero and inflate nothing, but the stem explicitly requires exclusion prior to calculation.

  • ✗

    { $sort: { purchaseAmount: 1 } }, { $group: { _id: "$customerId", total: { $sum: "$purchaseAmount" } } }

    Why it's wrong here

    $sort orders documents but removes nothing, so null or missing purchaseAmount values still reach $group, where $sum treats them as zero and includes those customers in the totals. Sorting is for ordering results, and would be right when the pipeline needs deterministic output order, not exclusion of incomplete records.

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.