Commit 8533dfb
fix(db-mongodb): remove duplicate IDs in nested relationship queries (#17019)
Backport of #16354 to 3.x.
## Summary
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`. Mongoose auto-casts 24-hex strings to ObjectId, so
both resolve to the same value, doubling the size of every `$in` query
(100 IDs -> 200 entries), causing larger queries, slower planning and,
in extreme cases, client disconnects.
The fix removes the redundant string push in `buildSearchParams.ts` and
keeps only `doc._id`, which `.lean()` already returns as the correct
BSON type.
## Test plan
- Regression test in `test/relationships/int.spec.ts` that queries
through a nested relationship with `or` conditions and asserts no
duplicate documents are returned.
Clean cherry-pick of the original commit, identical diff (+33/-2).
---------
Co-authored-by: Eduardo Costa <ed_cscosta@hotmail.com>
Co-authored-by: Sasha Rakhmatulin <sasha@ritsuko.dev>
Co-authored-by: German Jablonski <GermanJablo@users.noreply.github.com>1 parent b681448 commit 8533dfb
2 files changed
Lines changed: 33 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
221 | | - | |
222 | 220 | | |
223 | 221 | | |
| 222 | + | |
| 223 | + | |
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
473 | 473 | | |
474 | 474 | | |
475 | 475 | | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
476 | 507 | | |
477 | 508 | | |
478 | 509 | | |
| |||
0 commit comments