Skip to content
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

Sorting works incorrectly for some international characters with mongodb adapter #11137

Open
wuron opened this issue Feb 12, 2025 · 1 comment · May be fixed by #11242
Open

Sorting works incorrectly for some international characters with mongodb adapter #11137

wuron opened this issue Feb 12, 2025 · 1 comment · May be fixed by #11242
Assignees

Comments

@wuron
Copy link

wuron commented Feb 12, 2025

Describe the Bug

I'm developing an app in Ukrainian language and the sorting for some characters doesn't work as expected.
Having a posts collection with a single text field title, I'm trying to fetch them using local api:

const payload = await getPayload({ config });
const people = await payload.find({
  collection: 'posts',
  sort: ['title'],
});

The resulting documents are not sorted correctly.

I've seen a similar issue #6719 where it is suggested to change the mongo collation strength, but this doesn't resolve the issue, because the locale is just wrong.

I can get the documents with correct sorting using mongoose directly:

const documents = await payload.db.collections['posts'].find().sort('title').collation({
  locale: 'uk',
});

but the results are untyped and it feels like a hack using low level api for something that should work on high level.

I can see that payload.find accepts a locale param, but it doesn't change anything, and seems like it's used for i18n.
There's also a collation argument for mongooseAdapter but it does not accept locale.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload

Reproduction Steps

  1. Clone this repo and install dependencies. Basic template from reproduction guide is enough to reproduce this. No changes required.
  2. run pnpm dev _community
  3. Create 3 posts with the following titles: Роман, Віктор, Євген
  4. Run a local query for posts sorted by title or open http://localhost:3000/api/posts?sort=title

Expected result: ["Віктор", "Євген", "Роман"]
Actual result: ["Євген", "Віктор", "Роман"]

Which area(s) are affected? (Select all that apply)

db-mongodb

Environment Info

Node: 22.13.0
payload: 3.21.0
next: 15.1.6
@wuron wuron added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Feb 12, 2025
@paulpopus paulpopus self-assigned this Feb 12, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Feb 12, 2025
@paulpopus
Copy link
Contributor

Hey, we have a PR in review for this but just note that you will need to apply collation config in your mongodb adapter for locale to be applied from locale: 'uk' within your queries.

At a minimum you will need this once the fix has gone out. We thread the locale from your request into the underlying queries

collation: {
      strength: 1,
    },

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