From c520587077be83bfd4154d9d0e88753bb1ccfb66 Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Fri, 4 Aug 2023 15:57:00 -0700 Subject: [PATCH 1/2] Backport empty bulkwrite fix #13684 to Mongoose v6 --- lib/model.js | 26 +++++++++++++++----------- test/model.test.js | 9 +++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/model.js b/lib/model.js index 07bd9ef43b0..b4f6596f676 100644 --- a/lib/model.js +++ b/lib/model.js @@ -3772,17 +3772,21 @@ Model.bulkWrite = function(ops, options, callback) { let remaining = validations.length; let validOps = []; let validationErrors = []; - for (let i = 0; i < validations.length; ++i) { - validations[i]((err) => { - if (err == null) { - validOps.push(i); - } else { - validationErrors.push({ index: i, error: err }); - } - if (--remaining <= 0) { - completeUnorderedValidation.call(this); - } - }); + if (remaining === 0) { + completeUnorderedValidation.call(this) + } else { + for (let i = 0; i < validations.length; ++i) { + validations[i]((err) => { + if (err == null) { + validOps.push(i); + } else { + validationErrors.push({ index: i, error: err }); + } + if (--remaining <= 0) { + completeUnorderedValidation.call(this); + } + }); + } } validationErrors = validationErrors. diff --git a/test/model.test.js b/test/model.test.js index 848a1d6455b..b9b0ff0f3f6 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -7852,6 +7852,15 @@ describe('Model', function() { }); + it('Model.bulkWrite(...) does not hang with empty array and ordered: false (gh-13664)', async function() { + const userSchema = new Schema({ name: String }); + const User = db.model('User', userSchema); + + const err = await User.bulkWrite([], { ordered: false }).then(() => null, err => err); + assert.ok(err); + assert.equal(err.name, 'MongoInvalidArgumentError'); + }); + it('allows calling `create()` after `bulkWrite()` (gh-9350)', async function() { const schema = Schema({ foo: Boolean }); const Model = db.model('Test', schema); From 6f5adfbbcf24a56e64aab1f266ace7efab80a66a Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Fri, 4 Aug 2023 15:59:32 -0700 Subject: [PATCH 2/2] lint --- lib/model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index b4f6596f676..f304618a7ef 100644 --- a/lib/model.js +++ b/lib/model.js @@ -3773,7 +3773,7 @@ Model.bulkWrite = function(ops, options, callback) { let validOps = []; let validationErrors = []; if (remaining === 0) { - completeUnorderedValidation.call(this) + completeUnorderedValidation.call(this); } else { for (let i = 0; i < validations.length; ++i) { validations[i]((err) => {