From 3dd63336eb42113d8f7c9cbd453dbb735fb7951d Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Fri, 21 Apr 2023 18:46:47 -0400 Subject: [PATCH 1/4] Update document.test.js --- test/document.test.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/document.test.js b/test/document.test.js index a253a25b7a1..9fe35fee9cc 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -11867,6 +11867,18 @@ 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({ + coin: 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(); + assert.equal(doc.coins, 3000); + }); }); it('supports virtuals named `isValid` (gh-12124) (gh-6262)', async function() { @@ -12274,4 +12286,4 @@ describe('Check if instance function that is supplied in schema option is availa const TestDocument = new TestModel({}); assert.equal(TestDocument.instanceFn(), 'Returned from DocumentInstanceFn'); }); -}); +}); \ No newline at end of file From fa827ec3121197ee4db5787cc201d07c8d240f96 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 24 Apr 2023 11:19:00 -0400 Subject: [PATCH 2/4] add bluebird back --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 59fab87586e..445a1f32a05 100644 --- a/package.json +++ b/package.json @@ -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", From f067440fca8859bffaa60f049492152d41a4384e Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 24 Apr 2023 11:35:22 -0400 Subject: [PATCH 3/4] fix: calling $inc incessantly should work --- lib/document.js | 6 +++++- test/document.test.js | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/document.js b/lib/document.js index 7978dcb4bf6..96001581130 100644 --- a/lib/document.js +++ b/lib/document.js @@ -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); } diff --git a/test/document.test.js b/test/document.test.js index 9fe35fee9cc..3bf767cff8f 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -11869,7 +11869,7 @@ describe('document', function() { }); it('should correctly increment even if the document has not saved after each increment gh-13274', async function() { const schema = new Schema({ - coin: Number + coins: Number }); const Test = db.model('gh13274', schema); await Test.create({ coins: 0 }); @@ -11877,7 +11877,8 @@ describe('document', function() { doc.$inc('coins', 1000); doc.$inc('coins', 2000); await doc.save(); - assert.equal(doc.coins, 3000); + const check = await Test.findOne(); + assert.equal(check.coins, 3000); }); }); From 01baac18fbec70b52d307307bc217f1efec5c3c2 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 24 Apr 2023 11:36:46 -0400 Subject: [PATCH 4/4] lint fix --- test/document.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/document.test.js b/test/document.test.js index 3bf767cff8f..7dbc9cd7913 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12287,4 +12287,4 @@ describe('Check if instance function that is supplied in schema option is availa const TestDocument = new TestModel({}); assert.equal(TestDocument.instanceFn(), 'Returned from DocumentInstanceFn'); }); -}); \ No newline at end of file +});