Details
Description
The current implementation of SortStage is fully general in the sense that it can handle sorting all of the data that can be associated with a WorkingSetMember. This includes:
- Sorting WorkingSetMembers that represent index keys.
- Carrying RecordIds alongside the documents being sorted. This is necessary to support findAndModify with a sort. The RecordId is subsequently used by findAndModify to identify the document to update or delete.
- Carrying metadata alongside the documents being sorted. This could be textScore metadata, geoNear distance metadata, or sortKey metadata, for example.
This all comes with a cost. We can avoid sorting this unnecessary data in many cases, e.g. for sort operations other than findAndModify which don't involve metadata, and where the data has been fetched. This is done by introducing a new variant of SortStage which sorts simple BSON objects instead of entire WorkingSetMembers. An early prototype of this approach shows significant performance gains on several sort-related benchmarks.