fix(db-mongodb): remove duplicate IDs in nested relationship queries#17019
Open
GermanJablo wants to merge 3 commits into
Open
fix(db-mongodb): remove duplicate IDs in nested relationship queries#17019GermanJablo wants to merge 3 commits into
GermanJablo wants to merge 3 commits into
Conversation
…16354) Backport of #16354 to 3.x. ### What? When querying through a nested relationship (e.g. `where: { 'movie.name': { equals: '...' } }`), the `$in` array used to filter parent collection documents was populated with duplicate entries — each document ID was pushed twice: once as a string and once as a `Types.ObjectId`. ### Why? In `buildSearchParams.ts`, the `else` branch (for non-`join` fields) was doing: **Before:** ```ts const stringID = doc._id.toString() $in.push(stringID) if (Types.ObjectId.isValid(stringID)) { $in.push(doc._id) } ``` 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: ```ts $in.push(doc._id) ``` 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. --------- Co-authored-by: Sasha Rakhmatulin <sasha@ritsuko.dev> (cherry picked from commit fa8419e)
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
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.
Backport of #16354 to 3.x.
Summary
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. Mongoose auto-casts 24-hex strings to ObjectId, so both resolve to the same value, doubling the size of every$inquery (100 IDs -> 200 entries), causing larger queries, slower planning and, in extreme cases, client disconnects.The fix removes the redundant string push in
buildSearchParams.tsand keeps onlydoc._id, which.lean()already returns as the correct BSON type.Test plan
test/relationships/int.spec.tsthat queries through a nested relationship withorconditions and asserts no duplicate documents are returned.Clean cherry-pick of the original commit, identical diff (+33/-2).