Courseiva
Indexing →mediumMultiple Choice

C100DEV Indexing Practice Question

You have a collection of user activity logs where queries frequently filter by `userId` and sort by `timestamp` in descending order. Which single index definition provides optimal support for this query pattern without requiring an in-memory sort?

⚠ Common exam trap

Candidates often ignore the direction of the sort, assuming an index on {userId: 1, timestamp: 1} will work for a descending sort, which forces an expensive in-memory blocking sort.

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

✓

{ userId: 1, timestamp: -1 }

To avoid an in-memory sort, the index direction must match the query's sort direction for the sorted fields. Since `userId` is filtered via equality and `timestamp` is sorted descending, the index `{ userId: 1, timestamp: -1 }` allows MongoDB to traverse the index naturally in the required order, ensuring optimal query performance and preventing sorting memory limit errors during execution.

Answer analysis

Option-by-option breakdown

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

  • ✗

    { userId: 1, timestamp: 1 }

    Why it's wrong here

    An ascending sort order on the timestamp field contradicts the descending sort requirement of the query. MongoDB would need to perform an in-memory sort phase to reverse the order, which can cause query failures if the sort memory limit of 32 megabytes is exceeded.

  • ✗

    { timestamp: -1, userId: 1 }

    Why it's wrong here

    Placing the sorted field first violates the equality-first rule for compound indexes. Equality fields must always precede range or sort fields to ensure the B-tree traversal efficiently isolates the target documents before applying the requested sort ordering.

  • ✓

    { userId: 1, timestamp: -1 }

    Why this is correct

    A compound index on userId ascending then timestamp descending matches both the equality filter and the sort order, so MongoDB walks the index in the required descending sequence and returns results without an in-memory sort stage, satisfying the no-blocking-sort constraint.

  • ✗

    { userId: -1, timestamp: -1 }

    Why it's wrong here

    While equality checks on single values are direction-agnostic for index lookups, using a negative one for equality fields serves no functional benefit and obscures the logical intent of the schema design compared to standard ascending equality definitions.

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 →

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.