From b17db902a14f5c8d9854b00fe8cf57fe5e3e812f Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Fri, 3 Feb 2023 09:20:43 +0100 Subject: [PATCH 1/2] fix(document): isModified should not be triggered when setting a nested boolean to the same value as previosly fix #12992 --- lib/document.js | 2 +- test/document.test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/document.js b/lib/document.js index ffa48ce906f..f1ad40dfdbf 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1621,7 +1621,7 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru return false; } - if (!deepEqual(val, priorVal || utils.getValue(path, this))) { + if (!deepEqual(val, priorVal != null ? priorVal : utils.getValue(path, this))) { return true; } diff --git a/test/document.test.js b/test/document.test.js index 9804b6b8c18..80380ec26a9 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12075,6 +12075,41 @@ describe('document', function() { assert.equal(doc.list.length, 3); assert.deepStrictEqual(doc.list.map(el => el.a), [1, 2, 3]); }); + + it('should not trigger isModified when setting a nested boolean to the same value as previously (gh-12992)', async function() { + const Test = db.model('Test', new Schema({ + result: new Schema( + { + score: Number, + passed: Boolean + }, + { _id: false } + ) + })); + const newTest = await Test.create({ + result: { + score: 40, + passed: false + } + }); + + const existingTest = await Test.findById(newTest._id); + existingTest.result = { + score: 40, + passed: false + }; + + assert.equal(existingTest.isModified(), false); + assert.equal(existingTest.modifiedPaths().length, 0); + + existingTest.result = { + score: 40, + passed: true + }; + + assert.equal(existingTest.isModified(), true); + assert.equal(existingTest.modifiedPaths().length, 1); + }); }); describe('Check if instance function that is supplied in schema option is availabe', function() { From a69194fe3b009a08221f5d67dc437f40d49e7595 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 3 Feb 2023 17:37:28 -0500 Subject: [PATCH 2/2] Update document.js --- lib/document.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/document.js b/lib/document.js index f1ad40dfdbf..cd5c8b0c84e 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1621,7 +1621,7 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru return false; } - if (!deepEqual(val, priorVal != null ? priorVal : utils.getValue(path, this))) { + if (!deepEqual(val, priorVal !== undefined ? priorVal : utils.getValue(path, this))) { return true; }