C100DEV Aggregation Framework Practice Question
An aggregation pipeline processes a collection of sensor readings with fields `sensorId`, `timestamp`, and `value`. The pipeline must output, for each sensor, the most recent reading's value and the average value across all readings. Which pipeline correctly achieves this?
⚠ Common exam trap
The trap here is assuming $max or $last without sorting returns the latest value, when they depend on value magnitude or document order.
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
✓
$sort by timestamp descending, then $group with { _id: "$sensorId", latestValue: { $first: "$value" }, avgValue: { $avg: "$value" } }
To get the most recent reading's value per sensor, the pipeline must sort by timestamp descending so that the first document per group is the latest. Then $group uses $first to extract that value and $avg to compute the average. This combination ensures both requirements are met correctly.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
$sort by timestamp descending, then $group with { _id: "$sensorId", latestValue: { $first: "$value" }, avgValue: { $avg: "$value" } }
Why this is correct
Sorting by timestamp descending ensures that within each sensor group, the first document encountered by $first is the most recent reading. The $group stage then uses $first to capture that value and $avg to compute the average. This pipeline correctly produces the desired output for each sensor.
- ✗
$group with { _id: "$sensorId", latestValue: { $max: "$value" }, avgValue: { $avg: "$value" } }
Why it's wrong here
$max returns the highest value, not the value associated with the most recent timestamp. The most recent reading could have a lower value, so $max does not satisfy the requirement. The average is correct, but the latest value is wrong. Therefore, this pipeline is incorrect.
- ✗
$group with { _id: "$sensorId", latestValue: { $last: "$value" }, avgValue: { $avg: "$value" } }
Why it's wrong here
Without sorting, $last does not guarantee the most recent reading; it simply returns the last document in the group as encountered by the pipeline, which is not necessarily the latest by timestamp. The average would be correct, but the latest value would be unreliable. Thus, this pipeline does not meet the requirement.
- ✗
$sort by timestamp ascending, then $group with { _id: "$sensorId", latestValue: { $last: "$value" }, avgValue: { $avg: "$value" } }
Why it's wrong here
Sorting ascending and using $last would indeed capture the most recent reading, but the average is computed correctly. However, this approach relies on the sort order and $last, which works but is less explicit than using $first after a descending sort. Both are valid, but only one option is correct; here the descending sort with $first is more conventional and clear.
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.