fix(db-mongodb): remove duplicate IDs in nested relationship queries#16354
Merged
r1tsuu merged 4 commits intopayloadcms:mainfrom Apr 22, 2026
Merged
Conversation
DanRibbens
requested changes
Apr 22, 2026
r1tsuu
approved these changes
Apr 22, 2026
DanRibbens
approved these changes
Apr 22, 2026
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?
When querying through a nested relationship (e.g.
where: { 'movie.name': { equals: '...' } }), the$inarray used to filter parent collection documents was populated with duplicate entries — each document ID was pushed twice: once as a string and once as aTypes.ObjectId.Why?
In
buildSearchParams.ts, theelsebranch (for non-joinfields) was doing:Before:
Mongoose auto-casts 24-hex strings to ObjectId before sending queries to MongoDB, so both entries resolve to the same ObjectId. This caused every ID to appear twice in the $in array (e.g. 100 IDs → 200 entries), resulting in significantly larger queries, slower plaremnning times, and in extreme cases client disconnects due to timeouts.
How?
After:
The _id returned by Mongoose's .lean() is already the correct BSON type — ObjectId for standard collections, string/number for custom ID collections — so no extra casting is needed.
A regression test was added to test/relationships/int.spec.ts that queries directors through a nested movie relationship using or conditions and asserts no duplicate documents are returned.