-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Mongoose is creating subdocuments (and setting schema defaults) for fields that are undefined #10660
Comments
Is this issue #10505 talking about making my current issue the default behaviour. The author states that the issue he discusses went away in 5.13.4 |
This is a massive change in behaviour and people may not be aware that data is now being stored differently and their applications will start failing all of a sudden because of this change. As you can see from the picture below .. appointments with information were created using older version of Mongoose and appointments that are blank are using 6.0.3 and now 6.0.4 (same issue). |
It seem that using mongoose.set('setDefaultsOnInsert', false) stops the issue from occurring. However, I still believe the current implementation is confusing. Below I have a cut down schema representing Entity (sub document) and Appointment (owns the collection) ... As you can can see the Entity schema has a default for _id. The appointment schema is using the Entity schema for the property overhead but does not have a default value set on that property definition. When I read about breaking change setDefaultsOnInsert being true by default, I thought it would not affect us because the default was on the _id property of Entity and i was not setting the overhead property on appointment so we should get null in the database. If i had set overhead to some object then i would expect the default value to kick in on save because the overhead to some real value.
|
with 6.0, |
Hi @IslandRhythms, I understand that I can use that setting but I think the behaviour is dangerous which is why I did not close this issue myself. In my example schema above - overhead: {type: EntitySchema} does not have default set .. so i do not expect it be populated when the value for appointment.overhead = undefined. If I had provided a default like I did for - _id: { type: String, default: uuid.v4 } in the overhead schema, then i would have expected Mongoose to create a default value. In this case mongoose is ignoring the fact that no default was specified on overhead property ... It just went .. hey overhead is undefined and setDefaultsOnInsert = true so i should created the subdocument and take the default on the subdocument. A subdocument is not the same as number, string, boolean etc... it is mostly some sort of reference type and the developer has to make a decision to populate it and cannot exist as a value of _id and some other defaults. I honestly believe this setting is going to create insidious bugs for developers as its impact may not be known immediately. Also I wonder how many like me would have interpreted setDefaultsOnInsert to use the defaults on the Appointment schema and not subdocument schema. Regards, |
You're right that |
I'm still seeing this issue on a super important sub-document in my user code. Every user I save to the database ends up with the same empty schema:
Even if I force remove the variable
This is with |
@seantcanavan please open a new issue and follow the issue template |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Mongoose appears to be creating sub documents during update operations for fields that are undefined.
I am not sure if this is a version 6.x issue or latest version 5.x issue.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
The expected behaviour is that undefined is treated like null like in the past. There are now 1000's of places where we have to check for undefined values to ensure that invalid subdocuments are not automatically created.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 6.0.4
The text was updated successfully, but these errors were encountered: