Skip to content

Commit

Permalink
Merge pull request #13322 from Automattic/IslandRhythms/gh-13274
Browse files Browse the repository at this point in the history
Merge Document `$inc` calls instead of overwriting
  • Loading branch information
vkarpov15 authored Apr 25, 2023
2 parents fd4da49 + 01baac1 commit c9f5b19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,11 @@ Document.prototype.$inc = function $inc(path, val) {

if (shouldSet) {
this.$__.primitiveAtomics = this.$__.primitiveAtomics || {};
this.$__.primitiveAtomics[path] = { $inc: valToInc };
if (this.$__.primitiveAtomics[path] == null) {
this.$__.primitiveAtomics[path] = { $inc: valToInc };
} else {
this.$__.primitiveAtomics[path].$inc += valToInc;
}
this.markModified(path);
this.$__setValue(path, valToSet);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"axios": "1.1.3",
"babel-loader": "8.2.5",
"benchmark": "2.1.4",
"bluebird": "3.7.2",
"broken-link-checker": "^0.7.8",
"buffer": "^5.6.0",
"cheerio": "1.0.0-rc.12",
Expand Down
13 changes: 13 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11867,6 +11867,19 @@ describe('document', function() {
assert.ok(err);
assert.equal(err.errors['prop'].name, 'CastError');
});
it('should correctly increment even if the document has not saved after each increment gh-13274', async function() {
const schema = new Schema({
coins: Number
});
const Test = db.model('gh13274', schema);
await Test.create({ coins: 0 });
const doc = await Test.findOne();
doc.$inc('coins', 1000);
doc.$inc('coins', 2000);
await doc.save();
const check = await Test.findOne();
assert.equal(check.coins, 3000);
});
});

it('supports virtuals named `isValid` (gh-12124) (gh-6262)', async function() {
Expand Down

0 comments on commit c9f5b19

Please sign in to comment.