Skip to content

Commit 4bd5927

Browse files
committed
fix(document): consistently avoid marking subpaths of nested paths as modified
Fix #14022
1 parent 0d33ce5 commit 4bd5927

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
12271227

12281228
this.$__setValue(path, {});
12291229
for (const key of keys) {
1230-
this.$set(path + '.' + key, val[key], constructing, options);
1230+
this.$set(path + '.' + key, val[key], constructing, { ...options, _skipMarkModified: true });
12311231
}
12321232
if (priorVal != null &&
12331233
(!wasModified || hasInitialVal) &&

test/document.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,6 +3731,11 @@ describe('document', function() {
37313731

37323732
assert.deepEqual(
37333733
kitty.modifiedPaths(),
3734+
['surnames']
3735+
);
3736+
3737+
assert.deepEqual(
3738+
kitty.modifiedPaths({ includeChildren: true }),
37343739
['surnames', 'surnames.docarray']
37353740
);
37363741
});
@@ -12355,6 +12360,29 @@ describe('document', function() {
1235512360
const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 });
1235612361
assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A');
1235712362
});
12363+
12364+
it('avoids adding nested paths to markModified() output if adding a new field (gh-14024)', async function() {
12365+
const eventSchema = new Schema({
12366+
name: { type: String },
12367+
__stateBeforeSuspension: {
12368+
field1: { type: String },
12369+
field2: { type: String },
12370+
jsonField: {
12371+
name: { type: String },
12372+
name1: { type: String }
12373+
}
12374+
}
12375+
});
12376+
const Event = db.model('Event', eventSchema);
12377+
const eventObj = new Event({ name: 'event object', __stateBeforeSuspension: { field1: 'test', jsonField: { name: 'test3' } } });
12378+
await eventObj.save();
12379+
const newObject = { field1: 'test', jsonField: { name: 'test3', name1: 'test4' } };
12380+
eventObj.set('__stateBeforeSuspension', newObject);
12381+
assert.deepEqual(
12382+
eventObj.modifiedPaths(),
12383+
['__stateBeforeSuspension', '__stateBeforeSuspension.jsonField']
12384+
);
12385+
});
1235812386
});
1235912387

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

0 commit comments

Comments
 (0)