Mongoose is creating subdocuments (and setting schema defaults) for fields that are undefined
See original GitHub issueDo 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.
- Create a schema with a property that is an an embedded custom schema.
- Construct and update with with this property set to undefined (actually not set)
- The record will be created in the database and the property with the undefined value will be created with any defaults (defined in the schema set)
- If you repeat the steps and set the field specifically = null then this issue does not happen.
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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:9
Top Results From Across the Web
Mongoose v6.8.1: SubDocuments
Subdocument paths are undefined by default, and Mongoose does not apply subdocument defaults unless you set the subdocument path to a non-nullish value....
Read more >Mongoose cannot set property of " " to undefined,Whenever i ...
The Schema is declared right, but the you are trying to save author (subdocument) on the Schema itself, and not an actual document....
Read more >@prop | typegoose
Set a default, when no value is given at creation time. ... You may also set the default schema option to a function....
Read more >Understand Sub-documents & Nested documents in Mongoose
Subdocument paths are undefined by default, and Mongoose does not apply subdocument defaults unless you set the subdocument path to a non- ...
Read more >MongoDB Limits and Thresholds
The MongoDB Query Language is undefined over documents with duplicate field names. BSON builders may support creating a BSON document with duplicate field...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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, Tarek
You’re right that
setDefaultsOnInsert()
should ignore defaults underneath single nested paths. Fix will be in v6.0.7.