Skip to content

fix(db-mongodb): apply all keys of multi-key sort in join aggregations - #18260

Open
benjaminkincses wants to merge 1 commit into
payloadcms:mainfrom
benjaminkincses:fix/mongo-join-multi-key-sort
Open

benjaminkincses wants to merge 1 commit into
payloadcms:mainfrom
benjaminkincses:fix/mongo-join-multi-key-sort

Conversation

@benjaminkincses

@benjaminkincses benjaminkincses commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

What?

@payloadcms/db-mongodb's join aggregations applied only the first key of a multi-key sort. In packages/db-mongodb/src/utilities/buildJoinAggregation.ts, both the polymorphic and the standard join paths took Object.keys(sort)[0] from the record returned by buildSortParam() and discarded the rest, so a join field with defaultSort: ['-featured', 'name'] sorted by featured alone, and ties came back in insertion order instead of by name.

Why?

The Postgres (drizzle) adapter builds a full ORDER BY from every sort key, so the same config produced different ordering depending on the database. A plain find() with the same sort was also correct, since it passes the whole record through. Nothing warned that the join silently truncated the sort.

How?

  • Both join paths now convert the full buildSortParam() record into a MongoDB sort spec via a shared buildMultiKeySortSpec() helper, used for $sort (and $sortArray.sortBy in the polymorphic path). $sort and $sortArray.sortBy both accept multi-key specs natively.
  • The polymorphic path's $project stage now projects every sort key (except _id, which $project always includes, and relationTo, which is already projected) so the final $sortArray can sort on them, the previous projectSort boolean assumed a single key.
  • Added packages/db-mongodb/src/utilities/buildJoinAggregation.spec.ts with regression tests covering both join paths.

Fixes #18258

Test evidence

Ran the new buildJoinAggregation.spec.ts against the real buildJoinAggregation (unit-level, no DB needed):

  • Before the fix, both tests fail: the generated $sort / $sortArray.sortBy is { featured: -1 }, while buildSortParam() returned { featured: 'desc', name: 'asc', createdAt: 'desc' }, the -createdAt key is the fallback tiebreaker buildSortParam appends, also silently dropped.
  • After the fix, all pipeline stages contain { featured: -1, name: 1, createdAt: -1 }, and all 21 tests across the 5 db-mongodb spec files pass, including the 4 pre-existing suites.
  • eslint and prettier are clean on the changed files.

Follow-up (kept out of scope here): JoinQuery's per-join sort is typed string, while the field-level defaultSort is string | string[], the runtime already accepts arrays in both entry points, so the type could be widened to match.

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.

MongoDB join field applies only the first key of a multi-key sort

1 participant