From c8f61d8b598771ffe67ef222a510176af8938d00 Mon Sep 17 00:00:00 2001 From: Ian Blair Date: Fri, 6 Sep 2024 09:06:47 -0600 Subject: [PATCH] set merges deeply nested objects --- lib/document.js | 2 +- test/document.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/document.js b/lib/document.js index f6a6016c72..e62d43511b 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1213,7 +1213,7 @@ Document.prototype.$set = function $set(path, val, type, options) { this.$__setValue(path, null); cleanModifiedSubpaths(this, path); } else { - return this.$set(val, path, constructing); + return this.$set(val, path, constructing, options); } const keys = getKeysInSchemaOrder(this.$__schema, val, path); diff --git a/test/document.test.js b/test/document.test.js index 938b76a443..59d117aab1 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -8177,6 +8177,38 @@ describe('document', function() { await person.save(); }); + it('set() merge option with double nested', async function () { + const PersonSchema = new Schema({ + info: { + address: { + city: String, + country: { type: String, default: "UK" }, + postcode: String + }, + } + }); + + const Person = db.model('Person', PersonSchema); + + + const person = new Person({ + info: { + address: { + country: "United States", + city: "New York" + }, + } + }); + + const update = { info: { address: { postcode: "12H" } } }; + + person.set(update, undefined, { merge: true }); + + assert.equal(person.info.address.city, "New York"); + assert.equal(person.info.address.postcode, "12H"); + assert.equal(person.info.address.country, "United States"); + }); + it('setting single nested subdoc with timestamps (gh-8251)', async function() { const ActivitySchema = Schema({ description: String }, { timestamps: true }); const RequestSchema = Schema({ activity: ActivitySchema });