question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mongoose is creating subdocuments (and setting schema defaults) for fields that are undefined

See original GitHub issue

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.

  1. Create a schema with a property that is an an embedded custom schema.
  2. Construct and update with with this property set to undefined (actually not set)
  3. 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)
  4. 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:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:9

github_iconTop GitHub Comments

2reactions
thaoulacommented, Sep 3, 2021

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

1reaction
vkarpov15commented, Sep 18, 2021

You’re right that setDefaultsOnInsert() should ignore defaults underneath single nested paths. Fix will be in v6.0.7.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found