C100DEV Aggregation Framework Practice Question
A retail company stores order documents in a collection named orders. Each document has a field `total` (numeric) and a field `status` (string). The analytics team needs to produce a report that shows, for each `status`, the total revenue and the number of orders, but only for orders where `total` is greater than 100. The report must be sorted by total revenue in descending order. Which aggregation pipeline correctly produces this report?
⚠ Common exam trap
The trap here is assuming that $match can be placed after $group to filter on original fields, but after grouping the original fields are no longer present.
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
✓
db.orders.aggregate([ { $match: { total: { $gt: 100 } } }, { $group: { _id: "$status", totalRevenue: { $sum: "$total" }, orderCount: { $sum: 1 } } }, { $sort: { totalRevenue: -1 } } ])
The correct pipeline filters orders with total greater than 100 using $match before grouping. Then it groups by status, calculates total revenue with $sum on the total field, and counts orders with $sum: 1. Finally, it sorts the grouped results by totalRevenue in descending order. This order of stages is efficient and produces the required report.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
db.orders.aggregate([ { $group: { _id: "$status", totalRevenue: { $sum: "$total" }, orderCount: { $sum: 1 } } }, { $match: { total: { $gt: 100 } } }, { $sort: { totalRevenue: -1 } } ])
Why it's wrong here
Placing $match after $group is incorrect because after grouping, the documents no longer contain the original `total` field; they have `_id`, `totalRevenue`, and `orderCount`. The $match condition on `total` would not match any documents, resulting in an empty result set. Additionally, filtering after grouping is less efficient because it processes all orders before filtering. The correct order is to filter before grouping.
- ✓
db.orders.aggregate([ { $match: { total: { $gt: 100 } } }, { $group: { _id: "$status", totalRevenue: { $sum: "$total" }, orderCount: { $sum: 1 } } }, { $sort: { totalRevenue: -1 } } ])
Why this is correct
This pipeline first filters with $match, then groups by status and computes revenue and count, then sorts by totalRevenue descending. The $match stage uses an index if available, reducing the documents that reach $group. The $group stage correctly accumulates sum of total and count of documents, and $sort orders the results as required. This is the standard and efficient way to perform grouped aggregation with filtering and sorting.
- ✗
db.orders.aggregate([ { $match: { total: { $gt: 100 } } }, { $group: { _id: "$status", totalRevenue: { $sum: "$total" }, orderCount: { $count: {} } } }, { $sort: { totalRevenue: -1 } } ])
Why it's wrong here
The $group stage uses `{ $count: {} }` as an accumulator for orderCount. While $count is a valid accumulator in MongoDB 5.0+, it is used as a top-level stage or as an accumulator within $group? Actually, $count is not an accumulator; the correct accumulator for counting documents in a group is `{ $sum: 1 }`. Using $count inside $group is invalid and will cause an error. Therefore, this pipeline fails.
- ✗
db.orders.aggregate([ { $match: { total: { $gt: 100 } } }, { $group: { _id: "$status", totalRevenue: { $sum: "$total" }, orderCount: { $sum: 1 } } }, { $sort: { total: 1 } } ])
Why it's wrong here
This pipeline correctly filters and groups, but the final $sort stage sorts by `total` ascending. However, after $group, the field `total` no longer exists; the output documents have `totalRevenue` and `orderCount`. Sorting by a non-existent field has no effect, and the required descending order by total revenue is not achieved. The sort should use `totalRevenue: -1`.
About these practice questions
Courseiva writes every C100DEV question from scratch — 259 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.