Skip to content

fix(subdocuments): do not pass parent path on init#15970

Merged
vkarpov15 merged 3 commits intomasterfrom
gh-15969
Jan 17, 2026
Merged

fix(subdocuments): do not pass parent path on init#15970
vkarpov15 merged 3 commits intomasterfrom
gh-15969

Conversation

@AbdelrahmanHafez
Copy link
Copy Markdown
Collaborator

Fixes #15969
Re #15682

I'm not sure if this PR should be merged as is since it reverts more than just the bug fix, more of a discussion opener.

While working on something related to #15682, I discovered that just these few lines that were added to /lib/schema/subdocument.js

+    if (options.path != null) {
+      options = { ...options };
+      delete options.path;
+    }

Fix all the issues reported in #15678 as well as the bug in #15969, without the $pathRelativeToParent approach. I couldn't find anything wrong with this simpler approach, but I'm curious whether it breaks an architectural decision or fails some edge case I'm not aware of.

@vkarpov15 can you think of any reason not to use this instead?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request simplifies the fix for issues with nested map subdocuments by removing the $pathRelativeToParent optimization approach and instead deleting options.path before passing options to subdocument initialization. The PR addresses both issue #15969 (incorrect paths with deleteOne() and clear() on nested maps) and relates to #15682.

Changes:

  • Removes $pathRelativeToParent tracking from subdocuments and maps
  • Simplifies path calculation logic in map and subdocument classes
  • Adds options.path deletion in SchemaSubdocument.cast() before calling $init()
  • Adds tests for deleteOne() and clear() operations on nested map subdocuments

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
test/types.map.test.js Updates test assertions to use correct employee names and adds two new test cases for gh-15969 (deleteOne and clear operations)
lib/types/subdocument.js Removes $pathRelativeToParent property and related optimization logic, simplifies path calculation methods, but removes critical path deletion logic from constructor
lib/types/map.js Removes complex path calculation in constructor, eliminates $pathRelativeToParent tracking, simplifies set() method, adds Mixed schema type as default
lib/schema/map.js Adds options.path fallback and removes map path optimization logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/types/subdocument.js
Comment thread lib/types/subdocument.js
@AbdelrahmanHafez AbdelrahmanHafez marked this pull request as ready for review January 17, 2026 17:08
Copy link
Copy Markdown
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this new approach, much simpler. Tests all pass, fixes a bug, and slightly faster on mapOfSubdocs benchmark so I think this is a big improvement. Thanks!

Comment thread lib/types/map.js
pathToMark = fullPath.call(this);
}
parent.markModified(pathToMark);
const path = fullPath.call(this);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, the rationale for the way #15682 was implemented was for performance, specifically to avoid this fullPath() call where possible to avoid string concatenation. But that turns out to be incorrect because it looks like benchmarks/mapOfSubdocs is actually slightly faster with this PR than in master (372ms vs 395ms on my macbook)

@vkarpov15 vkarpov15 added this to the 9.1.5 milestone Jan 17, 2026
@vkarpov15 vkarpov15 merged commit e583614 into master Jan 17, 2026
50 checks passed
@vkarpov15 vkarpov15 deleted the gh-15969 branch January 17, 2026 21:19
vkarpov15 pushed a commit that referenced this pull request Jan 28, 2026
…maps (#15983)

* test(map): test document arrays in nested maps

* fix(documentArray): strip path from options in subdoc $init re #15350 #15678 #15970 #15682
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.

[BUG] deleteOne() and clear() silently fail on nested map subdocuments

3 participants