Skip to content

fix(db-mongodb): Do not add fallback sort if user sent a sort order #12888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

geminigeek
Copy link
Contributor

the current code pushes -createdAt sort even if user sent a sort order , which makes it dual sort and causes performance issue, only visible in large database, if user sent a sort do not add any other sort, fixes this #12690

the current code pushes createdAt sort even if user sent a sort order , which makes it dual sort and causes performance issue, if user sent a sort do not add any other sort, fixes this payloadcms#12690
@geminigeek geminigeek changed the title Update buildSortParam.ts fix: 12690 Mongodb Do not add fallback sort if user sent a sort order Jun 22, 2025
@geminigeek geminigeek changed the title fix: 12690 Mongodb Do not add fallback sort if user sent a sort order fix(db-mongodb): Do not add fallback sort if user sent a sort order Jun 22, 2025
Copy link
Contributor

Pull Request titles must follow the Conventional Commits specification and have valid scopes.

The subject "Do not add fallback sort if user sent a sort order" found in the pull request title "fix(db-mongodb): Do not add fallback sort if user sent a sort order"
didn't match the configured pattern. Please ensure that the subject
doesn't start with an uppercase character.

feat(ui): add Button component
^    ^    ^
|    |    |__ Subject
|    |_______ Scope
|____________ Type

Copy link
Contributor

@DanRibbens DanRibbens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other idea I can offer would be to add an additional db configuration arg to include disableFallbackSort or some other better name.

That way, for your implementation you'd have the choice because I can see why this would not be ideal.

One other idea that comes to mind, you could add all your field indexes as compound indexes with the createdAt: -1 so that sorting on the your collection is as performent as possible by the DB. That might actually be the best scenario and something we could look into.

What do you think?

@geminigeek
Copy link
Contributor Author

disableFallbackSort

hi,

disableFallbackSort is good idea but it should be implemented on payload config level or collection level.

Secondly some data/queries do not care about createdAt, current implementation just throw away the advantage of covered queries in MongodB https://www.mongodb.com/docs/manual/core/query-optimization/#covered-query

looking at the comment in PR which implemented this, seems to assume that user only creates index in payload which is wrong assumption.

whatever you guys choose to do, adding another sort when user sent an explicit sort is not right , if at all this is required it should be behind a flag not by default, current implementation makes all queries slow

@DanRibbens
Copy link
Contributor

What do you think @r1tsuu and @paulpopus?

@andershermansen
Copy link
Contributor

If you order by a unique field (or compound unique fields), it should at least not add any fallback.

@DanRibbens
Copy link
Contributor

If you order by a unique field (or compound unique fields), it should at least not add any fallback.

We don't have the field config at the this point in callstack without finding it in the collection config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants