-
-
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
Field values of {} are being discarded, but pass Schema on insert #14058
Comments
const mongoose = require('mongoose');
const BarSchema = new mongoose.Schema({
a: { type: String, required: false },
b: { type: String, required: false },
}, { _id: false });
const FooSchema = new mongoose.Schema({
name: { type: String, required: true },
bar: { type: BarSchema, required: true },
});
const Foo = mongoose.model('Foo', FooSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Foo.create({
name: 'hello',
bar: {}
});
console.log(await Foo.findOne());
const doc = await Foo.findOne();
doc.name = 'Test Testerson';
await doc.save();
}
run(); |
Stripping out empty objects is expected behavior with the minimize option. For example, with the following change, @IslandRhythms 's script succeeds: const FooSchema = new mongoose.Schema({
name: { type: String, required: true },
bar: { type: BarSchema, required: true },
}, { minimize: false }); That being said, I agree that As a workaround, I recommend adding a const FooSchema = new mongoose.Schema({
name: { type: String, required: true },
bar: { type: BarSchema, required: true, default: () => ({}) },
}); |
Upon further inspection, this is expected behavior that was introduced in 6.2.2 with #11247, and not something we can change without a backwards breaking change. Please add mongoose.Schema.Types.Subdocument.set('default', () => ({})); |
Will take another look in 7.6.7. This issue doesn't sit well with me, we'll put in some more cycles to see if we can work around it. |
Thanks, really appreciated. While the global default is probably nothing I'd want to do, I could live a specific default where it could happen. However, my gut feeling though is that introducing a breaking change may be worth it. TBH, if somebody injected |
fix: avoid minimizing single nested subdocs if they are required
Prerequisites
Mongoose version
7.6.2
Node.js version
20.x
MongoDB server version
Atlas
Typescript version (if applicable)
4.8.4
Description
Hi all
I wonder whether this is a newer issue, because we just had multiple cases of this in the past weeks, but never before:
Here's my schema:
Foo
entity, with abar
sub document, which is requiredBar
only has optional fieldsNext, I create an entity instance like this and create a document in the database:
Now, this
insert
passes validation, becausebar
is not null or undefined. However, upon inserting the document into the DB, this field is ignored, and thebar
field is missing in the created document.Accordingly, if I then fetch that document, it looks like this:
This means that subsequent updates of that document will no longer pass validation. So this fails with a runtime error:
Steps to Reproduce
See above: Create the an entry with a required field, where the value is
{}
. You'll see that this field is not persisted in a new document, but will pass validation.Expected Behavior
The empty object should be persisted on MongoDB. If I store an object that looks like this:
...then fetching it should give me the same result, which requires that
bar
is not stripped upon inserting the document into the DB.The text was updated successfully, but these errors were encountered: