Skip to content

Commit d533e9f

Browse files
authored
Merge pull request Automattic#14870 from ianHeydoc/Automatticgh-14861
set merges deeply nested objects
2 parents 8cfeb59 + c8f61d8 commit d533e9f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
12131213
this.$__setValue(path, null);
12141214
cleanModifiedSubpaths(this, path);
12151215
} else {
1216-
return this.$set(val, path, constructing);
1216+
return this.$set(val, path, constructing, options);
12171217
}
12181218

12191219
const keys = getKeysInSchemaOrder(this.$__schema, val, path);

test/document.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8177,6 +8177,38 @@ describe('document', function() {
81778177
await person.save();
81788178
});
81798179

8180+
it('set() merge option with double nested', async function () {
8181+
const PersonSchema = new Schema({
8182+
info: {
8183+
address: {
8184+
city: String,
8185+
country: { type: String, default: "UK" },
8186+
postcode: String
8187+
},
8188+
}
8189+
});
8190+
8191+
const Person = db.model('Person', PersonSchema);
8192+
8193+
8194+
const person = new Person({
8195+
info: {
8196+
address: {
8197+
country: "United States",
8198+
city: "New York"
8199+
},
8200+
}
8201+
});
8202+
8203+
const update = { info: { address: { postcode: "12H" } } };
8204+
8205+
person.set(update, undefined, { merge: true });
8206+
8207+
assert.equal(person.info.address.city, "New York");
8208+
assert.equal(person.info.address.postcode, "12H");
8209+
assert.equal(person.info.address.country, "United States");
8210+
});
8211+
81808212
it('setting single nested subdoc with timestamps (gh-8251)', async function() {
81818213
const ActivitySchema = Schema({ description: String }, { timestamps: true });
81828214
const RequestSchema = Schema({ activity: ActivitySchema });

0 commit comments

Comments
 (0)