<!-- llms-explorer concept facts · https://llms-explorer.com/tree/bounded-sort-internalboundedsort/ · pack 2026-10-22 · ~1791 tokens -->

# Bounded sort ($_internalBoundedSort)

> Source-anchored research for Bounded sort ($_internalBoundedSort). Run status: COMPLETED.

Parent: [MongoDB Time-Series Query and Index Optimization](https://llms-explorer.com/tree/mongodb-time-series-query-and-index-optimization/) · 5 facets · 13 facts · page: https://llms-explorer.com/tree/bounded-sort-internalboundedsort/

## Definitions

- Extended-range timestamps are an important boundary. The current PipelineD source rejects a control.min time-index path for collections marked as extended range. MongoDB issue records show valid pre-1970 time sorts and a $lookup inner sort throwing BoundedSorter input is too out-of-order. The bug tickets SERVER-94017 and SERVER-104719 are closed, while the related extended-range optimization ticket SERVER-70680 remains unresolved in the backlog. Not all these records list a fix version, so check the exact server release. (medium confidence) (verified as of 2026-10-22) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/pipeline/pipeline_d.cpp)

## How it works

- MongoDB's query rewrite notes describe the optimization as available from 6.0 when a time-series `$sort` on the timeField, or on metaField followed by timeField, is eligible. The query scans buckets in time order and `$_internalBoundedSort` interleaves measurements from overlapping buckets; the next bucket's time bound lets it emit already-buffered measurements that are earlier than any remaining input. (high confidence) — [source](https://github.com/mongodb/mongo/blob/master/src/mongo/db/query/timeseries/README.md)
- In the PipelineD source snapshot fetched on 2026-10-20, a pipeline sorted on the time field after unpacking is marked incompatible with SBE because bounded sort and streaming group are not implemented there yet. The same guard also prevents the streaming-group optimization on that path. (low confidence) (verified as of 2026-10-20) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/pipeline/pipeline_d.cpp)
- The current `PipelineD::supportsSort()` implementation only uses bounded sort when the chosen collection scan or index scan proves a compatible bucket ordering. The rewrite also requires the time field to survive unpacking, requires the metadata field for a compound metadata-plus-time sort, and rejects projections that change sort fields or special `$meta` sort keys. These are eligibility gates, not guarantees for every `$sort` on a time-series collection. (high confidence) (verified as of 2026-10-20) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/pipeline/pipeline_d.cpp)

## Measurements and reference values

- Percona's October 2021 MongoDB 5.0 benchmark reported a time-series aggregation running 40% faster than its regular-collection comparison. MongoDB's query rewrite notes date bounded sort to 6.0+, so the Percona result is workload-specific time-series context and does not measure the bounded-sort stage's speedup. (medium confidence) — [source](https://www.percona.com/blog/mongodb-5-0-time-series-collections/)

## Problems, failure modes and limitations

- A Stack Overflow report from November 2025 shows the same BoundedSorter input is too out-of-order error while sorting time-series data containing dates around year 2394. An answer recommends an index on the time field. Treat this as an individual report that points to checking date range and index selection, not as proof of a general defect. (low confidence) — [source](https://stackoverflow.com/questions/79822165/mongodb-timeseries-collection-cant-query-far-far-future-records)
- The current rewrite passes the absorbed `$sort` limit into `createBoundedSort()`. A historical MongoDB ticket records that the underlying `BoundedSorter` supported sort-limit optimization before the `$_internalBoundedSort` stage accepted a limit; that ticket was fixed in 6.0.0-rc5 and 5.0.15. (high confidence) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/pipeline/pipeline_d.cpp)
- The current MongoDB manual recommends a timeField index for time-series collections containing timestamps earlier than 1970-01-01 or later than 2038-01-19. In a November 2025 Stack Overflow report, an ascending sort with a year-2394 measurement raised `BoundedSorter input is too out-of-order`; an answer recommends adding the timeField index. Treat this as a version- and data-specific report, not a general current failure claim. (medium confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/manual/core/timeseries/timeseries-limitations/)

## Facts and statements

- The rewrite is plan-dependent. PipelineD rejects multikey index scans, requires compatible bucket-index order with the time field as the final sort key, and only supports recognized control.min or control.max time paths. The query design note also says modified sort fields block the rewrite and a matching hint may be needed for a {meta: 1, time: 1} sort. (high confidence) (verified as of 2026-10-20) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/pipeline/pipeline_d.cpp)
- A MongoDB Community explain from server 6.0.6 showed `$_internalBoundedSort` estimating 149,418,079 bytes of sorted data for 163,877 returned measurements, with no disk spill. The estimated total sorted-data bytes do not establish peak simultaneous buffering, and this single workload does not measure the stage against a full `$sort` on the same data. (medium confidence) (verified as of 2023-06-02) — [source](https://www.mongodb.com/community/forums/t/is-sorting-in-mongodb-supposed-to-be-very-slow-and-memory-consuming/229344)
- The current main-branch code declines every multikey IXSCAN for bounded sort. MongoDB issue SERVER-83348 proposes allowing the optimization for an equality predicate on a multikey metadata path, but the ticket remained unresolved in its 2026-09-25 update. Do not assume a metadata-array equality query will get this rewrite. (high confidence) (verified as of 2026-10-20) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/pipeline/pipeline_d.cpp)
- Time-series indexes order bucket records and store minimum and maximum values for measurement fields. MongoDB's query rewrite notes say the optimizer may need a hint to select the supporting index; its example for sorting by metadata and time uses a `{meta: 1, time: 1}` index. The manual recommends metadata indexes for equality and time-field indexes for ranges. (high confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/v8.3/core/timeseries/timeseries-index/)
- MongoDB issue SERVER-94017 recorded a valid ascending time sort across 1969 and 1970 measurements that raised an assertion attributed to rounded bucket time bounds; Jira marks it resolved as fixed by SERVER-94207. A separate `$lookup` reproduction in SERVER-104719 lists fixes in 8.2.0-rc0 and 8.0.20. These reports establish version-specific bug history, not a current universal defect. (medium confidence) (verified as of 2026-09-29) — [source](https://jira.mongodb.org/browse/SERVER-94017)
