Courseiva
Aggregation Framework →mediumMultiple Choice

C100DEV Aggregation Framework Practice Question

Why does using a $limit stage immediately after a $sort stage provide a performance advantage?

⚠ Common exam trap

Candidates often incorrectly assume that $limit after a $sort stage is just a standard filtering operation, missing the specific 'top-K' optimization that MongoDB applies to conserve memory.

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

✓

It allows the engine to keep only the top results in memory.

When $limit follows $sort, MongoDB uses a 'top-K' algorithm to maintain only the top N results in memory throughout the sort process. This prevents the database from needing to sort the entire collection in RAM, as it can discard documents that fall outside the sorted top-K range. This optimization is crucial for large datasets where sorting the full collection would exceed memory limits or significantly impact overall system throughput.

Answer analysis

Option-by-option breakdown

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

  • ✗

    It forces the use of a clustered index.

    Why it's wrong here

    The $limit stage does not dictate index usage; index usage is determined by the query planner based on the overall pipeline structure and available indexes. Relying on $limit after $sort does not change the storage engine's physical clustering, but rather changes how the aggregation engine handles data in memory.

  • ✓

    It allows the engine to keep only the top results in memory.

    Why this is correct

    By pairing these stages, MongoDB implements a top-K optimization. Instead of performing a full sort on the entire dataset, the database keeps only the top N documents in memory. This reduces memory pressure and CPU usage by avoiding unnecessary sorting work for documents that will never reach the final output.

  • ✗

    It automatically creates a temporary index.

    Why it's wrong here

    MongoDB does not create temporary indexes during the execution of an aggregation pipeline. The $sort stage relies on existing indexes or performs an in-memory sort. The performance gain from $limit comes from algorithmic optimization of memory usage, not from any automatic creation of database-level indexes on the fly.

  • ✗

    It disables the need for sorting entirely.

    Why it's wrong here

    The $sort stage is still mandatory to determine the top results. The presence of $limit does not eliminate the need for sorting; it merely optimizes the sorting process. Without the $sort stage, a $limit would simply return an arbitrary subset of documents rather than the top N records.

About these practice questions

This C100DEV question is part of Courseiva's 259-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.