Skip to content

Commit bb2eb40

Browse files
authored
Merge pull request #14504 from Automattic/vkarpov15/gh-14445
fix(document): make update minimization unset property rather than setting to null
2 parents eb2a52e + 1adf0e0 commit bb2eb40

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/model.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ Model.prototype.$__handleSave = function(options, callback) {
375375
}
376376
minimize(updateOp[key]);
377377
if (Object.keys(updateOp[key]).length === 0) {
378-
updateOp[key] = null;
378+
delete updateOp[key];
379+
update.$unset = update.$unset || {};
380+
update.$unset[key] = 1;
379381
}
380382
}
381383
}

test/document.test.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -13060,8 +13060,8 @@ describe('document', function() {
1306013060
x.metadata = {};
1306113061
await x.save();
1306213062

13063-
const { metadata } = await Model.findById(m._id).orFail();
13064-
assert.strictEqual(metadata, null);
13063+
const { metadata } = await Model.findById(m._id).lean().orFail();
13064+
assert.strictEqual(metadata, undefined);
1306513065
});
1306613066

1306713067
it('saves when setting subdocument to empty object (gh-14420) (gh-13782)', async function() {
@@ -13085,7 +13085,7 @@ describe('document', function() {
1308513085
await doc.save();
1308613086

1308713087
const savedDoc = await MainModel.findById(doc.id).orFail();
13088-
assert.strictEqual(savedDoc.sub, null);
13088+
assert.strictEqual(savedDoc.sub, undefined);
1308913089
});
1309013090

1309113091
it('validate supports validateAllPaths', async function() {
@@ -13205,6 +13205,37 @@ describe('document', function() {
1320513205
err.errors['docArr.0.subprop'].message
1320613206
);
1320713207
});
13208+
13209+
it('minimize unsets property rather than setting to null (gh-14445)', async function() {
13210+
const SubSchema = new mongoose.Schema({
13211+
name: { type: String }
13212+
}, { _id: false });
13213+
13214+
const MainSchema = new mongoose.Schema({
13215+
name: String,
13216+
sub: {
13217+
type: SubSchema,
13218+
default: {}
13219+
}
13220+
});
13221+
13222+
const Test = db.model('Test', MainSchema);
13223+
const doc = new Test({ name: 'foo' });
13224+
await doc.save();
13225+
13226+
const savedDocFirst = await Test.findById(doc.id).orFail();
13227+
assert.deepStrictEqual(savedDocFirst.toObject({ minimize: false }).sub, {});
13228+
13229+
savedDocFirst.name = 'bar';
13230+
await savedDocFirst.save();
13231+
13232+
const lean = await Test.findById(doc.id).lean().orFail();
13233+
assert.strictEqual(lean.sub, undefined);
13234+
13235+
const savedDocSecond = await Test.findById(doc.id).orFail();
13236+
assert.deepStrictEqual(savedDocSecond.toObject({ minimize: false }).sub, {});
13237+
13238+
});
1320813239
});
1320913240

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

0 commit comments

Comments
 (0)