What does the $limit stage do?
The $limit stage restricts the number of documents passed further down the pipeline. It's the analog of LIMIT in SQL.
How this works
- receives a stream of documents
- lets through only the specified number
- filters out the rest
Example
js
{ $limit: 10 }Only the first 10 documents continue down the pipeline.
Why it's used
- to avoid processing unnecessary data
- for paginated output (together with
$skip) - to optimize the pipeline by reducing the load on later stages
Summary
$limit is the stage that simply "trims" the stream and passes only the needed number of documents onward.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.