Skip to content

Commit 8b587cf

Browse files
committed
fix(model): correct handling for $push on a nested array
Fix #11108
1 parent 87bc411 commit 8b587cf

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/model.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -589,18 +589,22 @@ function operand(self, where, delta, data, val, op) {
589589
// editing the correct array element.
590590
// only increment the version if an array position changes.
591591
// modifying elements of an array is ok if position does not change.
592-
if (/\.\d+\.|\.\d+$/.test(data.path)) {
593-
// now handling $set, $unset
594-
// subpath of array
595-
self.$__.version = VERSION_WHERE;
596-
} else if (op === '$push' || op === '$addToSet' || op === '$pullAll' || op === '$pull') {
597-
self.$__.version = VERSION_INC;
592+
if (op === '$push' || op === '$addToSet' || op === '$pullAll' || op === '$pull') {
593+
if (/\.\d+\.|\.\d+$/.test(data.path)) {
594+
increment.call(self);
595+
} else {
596+
self.$__.version = VERSION_INC;
597+
}
598598
} else if (/^\$p/.test(op)) {
599599
// potentially changing array positions
600600
increment.call(self);
601601
} else if (Array.isArray(val)) {
602602
// $set an array
603603
increment.call(self);
604+
} else if (/\.\d+\.|\.\d+$/.test(data.path)) {
605+
// now handling $set, $unset
606+
// subpath of array
607+
self.$__.version = VERSION_WHERE;
604608
}
605609
}
606610

test/versioning.test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,16 @@ describe('versioning', function() {
664664
post1.comments = [{ likedBy: ['test'] }];
665665
await post1.save();
666666

667-
const comment = post2.comments[0];
667+
let comment = post2.comments[0];
668668
comment.likedBy.push('Some User');
669669

670670
const err = await post2.save().then(() => null, err => err);
671671
assert.equal(err.name, 'VersionError');
672+
673+
const post3 = await Test.findById(entry._id).exec();
674+
comment = post3.comments[0];
675+
comment.likedBy.push('Some User');
676+
await post3.save();
677+
assert.equal(post3.__v, 2);
672678
});
673679
});

0 commit comments

Comments
 (0)