Skip to content

Latest commit

 

History

History
10 lines (9 loc) · 566 Bytes

fact-agg-sort-limit.rst

File metadata and controls

10 lines (9 loc) · 566 Bytes

When a :pipeline:`$sort` immediately precedes a :pipeline:`$limit` in the pipeline, the :pipeline:`$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 allowDiskUse is true and the n items exceed the :ref:`aggregation memory limit <agg-memory-restrictions>`.

.. versionchanged:: 2.4
   Before MongoDB 2.4, :pipeline:`$sort` would sort all the results in
   memory, and then limit the results to n results.