Repository navigation
fix(query): avoid path collision when excluding subdocument with select: false child (backport to 8.x) - #16604
Merged
vkarpov15 merged 1 commit intoOct 20, 2026
Conversation
…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
approved these changes
Oct 20, 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.
Summary
Backport of #16409 to 8.x (gh-12798).
On 8.x,
.select('-subd')on a schema wheresubd.rawhasselect: falsemakesapplyPaths()build{ subd: 0, 'subd.raw': 0 }. MongoDB rejects that projection with a path collision error, even thoughsubd: 0already coverssubd.raw. The fix skips a schema-levelselect: falsepath 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 fullnpm testsuite (4046 passing, 0 failing, 50 pending), and ESLint on the changed files.