Skip to content

Encode a ranked accumulator's sortBy as one document - #4571

Open
evanchooly wants to merge 1 commit into
audits-snippetsfrom
audits-ranked-sortby
Open

evanchooly wants to merge 1 commit into
audits-snippetsfrom
audits-ranked-sortby

Conversation

@evanchooly

Copy link
Copy Markdown
Member

Found by the operator-audit generator, while generating the $topN and $bottomN documented examples (next PR in the stack). Stacked on #4570.

Problem

$top, $bottom, $topN and $bottomN with more than one sort key encoded sortBy as an array of one-key documents:

// topN(3, array("$title", "$imdb.rating"), descending("imdb.rating"), ascending("title"))
{ $topN: { output: [...], sortBy: [ { "imdb.rating": -1 }, { title: 1 } ], n: 3 } }

The server takes one document whose keys are in priority order, and rejects the array:

{ $topN: { output: [...], sortBy: { "imdb.rating": -1, title: 1 }, n: 3 } }

Only a single sort key worked, which is why the existing fixtures (all single-key) never caught it. The manual's "Find the Three Highest-Rated Movies in a Single Genre" $topN example uses two keys.

Change

RankedResultsExpressionCodec and NRankedResultsExpressionCodec always write sortBy as one document, with the keys in the order given.

Verification

  • New TestRankedSortBy: topN and bottom with two keys encode one ordered document, and the server accepts the pipeline and breaks a tie on the second key. It fails on the old codecs: expected <{score=-1, player=1}> but was <[{score=-1}, {player=1}]>.
  • TestTop, TestTopN, TestBottom and TestBottomN still pass under Critter, then reflection.

No docs change: the API is unchanged.

Self-reviewed only: the independent reviewers are out of quota until 2026-11-26.

$top, $bottom, $topN and $bottomN with more than one sort key wrote
sortBy as an array of one-key documents, which the server rejects; it
takes one document whose keys are in priority order.
evanchooly added a commit that referenced this pull request Nov 23, 2026
Backport of #4571 to 2.5.x.

## Problem

`$top`, `$bottom`, `$topN` and `$bottomN` with more than one sort key
encoded `sortBy` as an array of one-key documents, `[{score: -1},
{player: 1}]`. The server takes one document whose keys are in priority
order, `{score: -1, player: 1}`, and rejects the array. A single sort
key worked.

## Change

`RankedResultsExpression` and `NRankedResultsExpression` always write
`sortBy` as one document. The sort orders stay Int64, as 2.5 writes
them; the Int32 change is 3.0 only.

## Verification

- New `TestRankedSortBy`: `topN` and `bottom` with two keys encode one
ordered document, and the server accepts the pipeline and breaks a tie
on the second key. It fails without the fix.
- `TestTopN`, `TestBottomN`, `TestFirstN` and `TestLastN` still pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant