Skip to content

Commit

Permalink
fix(document): allow setting nested path to null
Browse files Browse the repository at this point in the history
Fix #14205
  • Loading branch information
vkarpov15 committed Jan 2, 2024
1 parent 8e141d1 commit 6d526cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
} else {
throw new StrictModeError(key);
}
} else if (pathtype === 'nested' && valForKey === null) {
this.$set(pathName, valForKey, constructing, options);
}
} else if (valForKey !== void 0) {
this.$set(pathName, valForKey, constructing, options);
Expand Down
19 changes: 19 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12437,6 +12437,25 @@ describe('document', function() {
const fromDb = await Test.findById(eventObj._id).lean().orFail();
assert.strictEqual(fromDb.__stateBeforeSuspension.field3['.ippo'], 5);
});

it('handles setting nested path to null (gh-14205)', function() {
const schema = new mongoose.Schema({
nested: {
key1: String,
key2: String
}
});

const Model = db.model('Test', schema);

const doc = new Model();
doc.init({
nested: { key1: 'foo', key2: 'bar' }
});

doc.set({ nested: null });
assert.strictEqual(doc.toObject().nested, null);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit 6d526cd

Please sign in to comment.