Skip to content

Commit

Permalink
fix(document): allow setting values to undefined with set(obj) syntax…
Browse files Browse the repository at this point in the history
… with strict: false

Fix #15192
vkarpov15 committed Jan 27, 2025
1 parent 1020947 commit fd11431
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
@@ -1147,7 +1147,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
} else if (pathtype === 'nested' && valForKey == null) {
this.$set(pathName, valForKey, constructing, options);
}
} else if (valForKey !== void 0) {
} else {
this.$set(pathName, valForKey, constructing, options);
}
}
21 changes: 21 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
@@ -7363,6 +7363,27 @@ describe('document', function() {
assert.strictEqual(obj.subDoc.timestamp, date);
});

it('supports setting values to undefined with strict: false (gh-15192)', async function() {
const helloSchema = new mongoose.Schema({
name: { type: String, required: true },
status: { type: Boolean, required: true },
optional: { type: Number }
}, { strict: false });
const Hello = db.model('Test', helloSchema);

const obj = new Hello({ name: 'abc', status: true, optional: 1 });
const doc = await obj.save();

doc.set({ optional: undefined });

assert.ok(doc.isModified());

await doc.save();

const { optional } = await Hello.findById(doc._id).orFail();
assert.strictEqual(optional, undefined);
});

it('handles .set() on doc array within embedded discriminator (gh-7656)', function() {
const pageElementSchema = new Schema({
type: { type: String, required: true }

0 comments on commit fd11431

Please sign in to comment.