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); 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 + } + ); + }); + }); });