-
-
Couldn't load subscription status.
- Fork 3.9k
Avoid leaving subdoc defaults on top-level doc when setting subdocument to same value #14728
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
Conversation
…tting subdocument to same value Fix #14722
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: "[email protected]"
});
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 |
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
defaultstate 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
defaultstate, even though we modify the document array.Examples