Courseiva
Aggregation Framework →mediumMultiple Choice

C100DEV Aggregation Framework Practice Question

You maintain a MongoDB collection named `readings` that stores hourly sensor measurements. Each document includes a `sensorId` string, a `recordedAt` date, and a `temperature` numeric value. You need to produce a report that, for each sensor, lists the sensor identifier alongside the timestamp of its single highest temperature reading. Duplicate temperatures are possible, and in that case any one of the tied readings is acceptable. Which aggregation pipeline should you run?

⚠ Common exam trap

The trap here is assuming $first or $last selects the minimum or maximum value automatically, when they simply take the first or last document in the current stream 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

✓

db.readings.aggregate([ { $sort: { sensorId: 1, temperature: -1 } }, { $group: { _id: "$sensorId", recordedAt: { $first: "$recordedAt" }, temperature: { $first: "$temperature" } } } ])

Selecting the top record per group requires ordering the input before the grouping stage, because accumulators such as $first operate on the incoming document stream order. Sorting by the group key and then by the ranking field descending guarantees the desired record arrives first for each group, letting $first extract both the timestamp and the measurement together.

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.readings.aggregate([ { $group: { _id: "$sensorId", temperatures: { $push: "$temperature" }, times: { $push: "$recordedAt" } } }, { $sort: { temperatures: -1 } } ])

    Why it's wrong here

    Pushing temperatures and timestamps into parallel arrays does not preserve a reliable index alignment after grouping, and sorting arrays compares them lexicographically rather than selecting a single maximum. The pipeline never reduces each sensor to one document with its peak reading, so the requested report shape is not produced.

  • ✗

    db.readings.aggregate([ { $sort: { temperature: -1 } }, { $group: { _id: "$sensorId", recordedAt: { $last: "$recordedAt" }, temperature: { $last: "$temperature" } } } ])

    Why it's wrong here

    Sorting globally by temperature descending followed by $group means $last picks the final document seen per sensor, which is the lowest temperature for that sensor, not the highest. The sort key also fails to establish per-sensor ordering of the top value, so the pairing of recordedAt and temperature is wrong.

  • ✗

    db.readings.aggregate([ { $group: { _id: "$sensorId", recordedAt: { $first: "$recordedAt" }, temperature: { $first: "$temperature" } } }, { $sort: { temperature: -1 } } ])

    Why it's wrong here

    $first inside $group returns a field from the first document the group stage encounters, which depends on natural storage order, not on temperature. Sorting after grouping only orders the reduced output documents; it cannot recover the timestamp belonging to the maximum temperature, so the reported recordedAt may not correspond to the highest reading at all.

  • ✓

    db.readings.aggregate([ { $sort: { sensorId: 1, temperature: -1 } }, { $group: { _id: "$sensorId", recordedAt: { $first: "$recordedAt" }, temperature: { $first: "$temperature" } } } ])

    Why this is correct

    The $sort stage orders documents by sensorId ascending and temperature descending, so within each sensor the highest temperature comes first. The subsequent $group with $first then captures that leading document's recordedAt and temperature while grouping by sensorId. This is the canonical top-one-per-group pattern in the aggregation framework and satisfies the requirement exactly.

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.