From eabdccb2e66dda812abdbbcb3b233cd66a79309a Mon Sep 17 00:00:00 2001 From: Hafez Date: Sun, 14 Jun 2020 11:06:48 +0200 Subject: [PATCH 1/3] test(model): repro #9131 --- test/model.test.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/test/model.test.js b/test/model.test.js index 01ad32e4f74..9d674f158c5 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -6799,10 +6799,44 @@ describe('Model', function() { assert.equal(typeof users[0].updatedAt, 'number'); assert.equal(typeof users[1].updatedAt, 'number'); - // not-lean queries casts to number even if stored on DB as a date + // not-lean queries cast to number even if stored on DB as a date assert.equal(users[0] instanceof User, false); assert.equal(users[1] instanceof User, false); }); }); + it('Model#bulkWrite(...) does not throw an error when provided an empty array (gh-9131)', function() { + return co(function*() { + const userSchema = new Schema(); + const User = db.model('User', userSchema); + + const res = yield User.bulkWrite([]); + + assert.deepEqual( + res, + { + result: { + ok: 1, + writeErrors: [], + writeConcernErrors: [], + insertedIds: [], + nInserted: 0, + nUpserted: 0, + nMatched: 0, + nModified: 0, + nRemoved: 0, + upserted: [] + }, + insertedCount: 0, + matchedCount: 0, + modifiedCount: 0, + deletedCount: 0, + upsertedCount: 0, + upsertedIds: {}, + insertedIds: {}, + n: 0 + } + ); + }); + }); }); From 1ee8bc2303087876ee2d498c95e4cfdc63a378f2 Mon Sep 17 00:00:00 2001 From: Hafez Date: Sun, 14 Jun 2020 11:16:42 +0200 Subject: [PATCH 2/3] fix(model): allow empty arrays for bulkWrite --- lib/helpers/getDefaultBulkwriteResult.js | 27 ++++++++++++++++++++++++ lib/model.js | 6 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 lib/helpers/getDefaultBulkwriteResult.js diff --git a/lib/helpers/getDefaultBulkwriteResult.js b/lib/helpers/getDefaultBulkwriteResult.js new file mode 100644 index 00000000000..7d10f174864 --- /dev/null +++ b/lib/helpers/getDefaultBulkwriteResult.js @@ -0,0 +1,27 @@ +'use strict'; +function getDefaultBulkwriteResult() { + return { + result: { + ok: 1, + writeErrors: [], + writeConcernErrors: [], + insertedIds: [], + nInserted: 0, + nUpserted: 0, + nMatched: 0, + nModified: 0, + nRemoved: 0, + upserted: [] + }, + insertedCount: 0, + matchedCount: 0, + modifiedCount: 0, + deletedCount: 0, + upsertedCount: 0, + upsertedIds: {}, + insertedIds: {}, + n: 0 + }; +} + +module.exports = getDefaultBulkwriteResult; \ No newline at end of file diff --git a/lib/model.js b/lib/model.js index 16a1f56f6e6..63eb74c3a15 100644 --- a/lib/model.js +++ b/lib/model.js @@ -31,6 +31,7 @@ const applyStatics = require('./helpers/model/applyStatics'); const applyWriteConcern = require('./helpers/schema/applyWriteConcern'); const assignVals = require('./helpers/populate/assignVals'); const castBulkWrite = require('./helpers/model/castBulkWrite'); +const getDefaultBulkwriteResult = require('./helpers/getDefaultBulkwriteResult'); const discriminator = require('./helpers/model/discriminator'); const each = require('./helpers/each'); const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue'); @@ -3493,7 +3494,6 @@ Model.bulkWrite = function(ops, options, callback) { const validations = ops.map(op => castBulkWrite(this, op, options)); callback = this.$handleCallbackError(callback); - return promiseOrCallback(callback, cb => { cb = this.$wrapCallback(cb); each(validations, (fn, cb) => fn(cb), error => { @@ -3501,6 +3501,10 @@ Model.bulkWrite = function(ops, options, callback) { return cb(error); } + if (ops.length === 0) { + return cb(null, getDefaultBulkwriteResult()); + } + this.collection.bulkWrite(ops, options, (error, res) => { if (error) { return cb(error); From d7f10689e7095945b537fd01a5bc32ffc5ea5eed Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 15 Jun 2020 11:24:40 -0400 Subject: [PATCH 3/3] chore: release 5.9.19 --- History.md | 16 ++++++++++++++++ package.json | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index abc5cc12655..4215c6aab93 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,19 @@ +5.9.19 / 2020-06-15 +=================== + * fix: upgrade mongodb driver -> 3.5.9 #9124 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez) + * fix: copy `required` validator on single nested subdoc correctly when calling `Schema#clone()` #8819 + * fix(discriminator): handle `tiedValue` when casting update on nested paths #9108 + * fix(model): allow empty arrays for bulkWrite #9132 #9131 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez) + * fix(schema): correctly set partialFilterExpression for nested schema indexes #9091 + * fix(castArrayFilters): handle casting on all fields of array filter #9122 [lafeuil](https://github.com/lafeuil) + * fix(update): handle nested path createdAt when overwriting parent path #9105 + * docs(subdocs): add some notes on the difference between single nested subdocs and nested paths #9085 + * docs(subdocs): improve docs on `typePojoToMixed` #9085 + * docs: add note about connections in `globalSetup` with Jest #9063 + * docs: add schema and how to set default sub-schema to schematype options #9111 [dfle](https://github.com/dfle) + * docs(index): use `const` instead of `var` in examples #9125 [dmcgrouther](https://github.com/dmcgrouther) + * docs: corrected markdown typo #9117 + 5.9.18 / 2020-06-05 =================== * fix: improve atlas error in the event of incorrect password #9095 diff --git a/package.json b/package.json index a719c94ab89..b99db2db5c1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mongoose", "description": "Mongoose MongoDB ODM", - "version": "5.9.18", + "version": "5.9.19", "author": "Guillermo Rauch ", "keywords": [ "mongodb",