Repository navigation
fix(db-mongodb): apply all keys of multi-key sort in join aggregations - #18260
Open
benjaminkincses wants to merge 1 commit into
Open
benjaminkincses wants to merge 1 commit into
benjaminkincses wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
@payloadcms/db-mongodb's join aggregations applied only the first key of a multi-key sort. Inpackages/db-mongodb/src/utilities/buildJoinAggregation.ts, both the polymorphic and the standard join paths tookObject.keys(sort)[0]from the record returned bybuildSortParam()and discarded the rest, so a join field withdefaultSort: ['-featured', 'name']sorted byfeaturedalone, and ties came back in insertion order instead of byname.Why?
The Postgres (drizzle) adapter builds a full
ORDER BYfrom every sort key, so the same config produced different ordering depending on the database. A plainfind()with the same sort was also correct, since it passes the whole record through. Nothing warned that the join silently truncated the sort.How?
buildSortParam()record into a MongoDB sort spec via a sharedbuildMultiKeySortSpec()helper, used for$sort(and$sortArray.sortByin the polymorphic path).$sortand$sortArray.sortByboth accept multi-key specs natively.$projectstage now projects every sort key (except_id, which$projectalways includes, andrelationTo, which is already projected) so the final$sortArraycan sort on them, the previousprojectSortboolean assumed a single key.packages/db-mongodb/src/utilities/buildJoinAggregation.spec.tswith regression tests covering both join paths.Fixes #18258
Test evidence
Ran the new
buildJoinAggregation.spec.tsagainst the realbuildJoinAggregation(unit-level, no DB needed):$sort/$sortArray.sortByis{ featured: -1 }, whilebuildSortParam()returned{ featured: 'desc', name: 'asc', createdAt: 'desc' }, the-createdAtkey is the fallback tiebreakerbuildSortParamappends, also silently dropped.{ featured: -1, name: 1, createdAt: -1 }, and all 21 tests across the 5db-mongodbspec files pass, including the 4 pre-existing suites.eslintandprettierare clean on the changed files.Follow-up (kept out of scope here):
JoinQuery's per-joinsortis typedstring, while the field-leveldefaultSortisstring | string[], the runtime already accepts arrays in both entry points, so the type could be widened to match.