-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid leaving subdoc defaults on top-level doc when setting subdocument to same value #14728
Merged
Conversation
This file contains 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
…tting subdocument to same value Fix #14722
hasezoey
reviewed
Jul 6, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont fully understand what is fixed here, but the code looks good to me.
Lol. To put it another way, the issue is that, when you do the following, const getUser = () => ({
_id: new mongoose.Types.ObjectId("66852317541ef0e22ae5214c"),
name: "blowfishlol",
email: "blowfish@test.com"
});
const { _id } = await TestModel.create({ name: 'foobar', updater: getUser() });
const user = await TestModel.findById(_id).orFail();
user.set('updater', getUser());
user.$isDefault('updater._id'); // true without this PR, which shouldn't happen since we set `updater` to the same value |
This was referenced Aug 2, 2024
This was referenced Aug 3, 2024
This was referenced Aug 4, 2024
This was referenced Sep 4, 2024
This was referenced Sep 11, 2024
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
The cause of #14722 is that, when creating a new subdocument, Mongoose first applies defaults to the subdocument, and those defaults bubble up to the top-level document. Which leaves any default paths in
default
state on the top-level document, even if we're setting the subdocument to the exact same value.This PR makes it so that we skip parent change tracking on defaults when creating a completely new subdocument - the idea is, if we're creating a new subdocument, we'll be setting the subdocument to the top-level document, so we will track the change as "set the subdocument" rather than "set the subdocument, and also set this default on the subdocument."
There's also some followup work I want to do related to #4145: #4145 succeeds because we leave document array underneath a nested path in
default
state, even though we modify the document array.Examples