Consider the following example:
This operation returns only the first 5 documents passed to it by the pipeline.
$limit has no effect on the content of the documents it passes.
$sort immediately precedes a
$limit in the pipeline, the
$sort operation only maintains the top
n results as it progresses, where
n is the specified limit, and MongoDB only needs to store
n items in memory. This optimization still applies when
true and the
n items exceed the aggregation memory limit.