Skip to content

fix(query): avoid path collision when excluding subdocument with select: false child (backport to 8.x) - #16604

Merged
vkarpov15 merged 1 commit into
Automattic:8.xfrom
mrityunjay-pandey:backport-8x/select-false-subdoc-collision
Oct 20, 2026
Merged

vkarpov15 merged 1 commit into
Automattic:8.xfrom
mrityunjay-pandey:backport-8x/select-false-subdoc-collision

Conversation

@mrityunjay-pandey

Copy link
Copy Markdown

Summary

Backport of #16409 to 8.x (gh-12798).

On 8.x, .select('-subd') on a schema where subd.raw has select: false makes applyPaths() build { subd: 0, 'subd.raw': 0 }. MongoDB rejects that projection with a path collision error, even though subd: 0 already covers subd.raw. The fix skips a schema-level select: false path when one of its ancestors is already excluded. The check walks the path's own segments, so it stays O(depth).

It cherry-picks cleanly onto 8.x (git cherry-pick -x 84799ed67), no changes needed.

Examples

The test added in #16409 is included. On current 8.x (8.24.5), excluding a subdocument path does not conflict with a nested select: false path (gh-12798) fails; with this change it passes.

Ran locally on Linux, Node 24: test/query.test.js, the full npm test suite (4046 passing, 0 failing, 50 pending), and ESLint on the changed files.

…ct: false child (Automatticgh-12798)

`applyPaths()` was projecting schema-level `select: false` paths onto
`fields` unconditionally, even when an ancestor path was already
excluded by the user (e.g. `.select('-subd')` plus `subd.raw`'s
schema-level `select: false`). That produces both `subd: 0` and
`subd.raw: 0`, which MongoDB rejects as a path collision.

Skip projecting a schema-excluded path when one of its ancestors is
already excluded in `fields` -- the ancestor exclusion already covers
it. O(depth) check, no scan over the full projection.

(cherry picked from commit 84799ed)
@vkarpov15 vkarpov15 added this to the 8.24.6 milestone Oct 20, 2026
@vkarpov15
vkarpov15 merged commit b6ce9eb into Automattic:8.x Oct 20, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants