Skip to content

fix(db-mongodb): remove duplicate IDs in nested relationship queries#16354

Merged
r1tsuu merged 4 commits intopayloadcms:mainfrom
ed-cscosta:fix/db-mongodb-duplicate-ids-in-query
Apr 22, 2026
Merged

fix(db-mongodb): remove duplicate IDs in nested relationship queries#16354
r1tsuu merged 4 commits intopayloadcms:mainfrom
ed-cscosta:fix/db-mongodb-duplicate-ids-in-query

Conversation

@ed-cscosta
Copy link
Copy Markdown
Contributor

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:

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:

$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.

@ed-cscosta ed-cscosta changed the title fix(db-mongodb): remove duplicate ObjectIds in nested relationship $in queries fix(db-mongodb): remove duplicate IDs in nested relationship queries Apr 22, 2026
Comment thread packages/db-mongodb/src/queries/buildSearchParams.ts Outdated
@DanRibbens DanRibbens requested a review from r1tsuu April 22, 2026 15:19
@r1tsuu r1tsuu enabled auto-merge (squash) April 22, 2026 16:03
@r1tsuu r1tsuu merged commit fa8419e into payloadcms:main Apr 22, 2026
167 checks passed
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