Skip to content

Commit d289f07

Browse files
authored
Merge pull request #9132 from AbdelrahmanHafez/gh-9131
fix(model): allow empty arrays for bulkWrite
2 parents 075efbd + 1ee8bc2 commit d289f07

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
function getDefaultBulkwriteResult() {
3+
return {
4+
result: {
5+
ok: 1,
6+
writeErrors: [],
7+
writeConcernErrors: [],
8+
insertedIds: [],
9+
nInserted: 0,
10+
nUpserted: 0,
11+
nMatched: 0,
12+
nModified: 0,
13+
nRemoved: 0,
14+
upserted: []
15+
},
16+
insertedCount: 0,
17+
matchedCount: 0,
18+
modifiedCount: 0,
19+
deletedCount: 0,
20+
upsertedCount: 0,
21+
upsertedIds: {},
22+
insertedIds: {},
23+
n: 0
24+
};
25+
}
26+
27+
module.exports = getDefaultBulkwriteResult;

lib/model.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const applyStatics = require('./helpers/model/applyStatics');
3131
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
3232
const assignVals = require('./helpers/populate/assignVals');
3333
const castBulkWrite = require('./helpers/model/castBulkWrite');
34+
const getDefaultBulkwriteResult = require('./helpers/getDefaultBulkwriteResult');
3435
const discriminator = require('./helpers/model/discriminator');
3536
const each = require('./helpers/each');
3637
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
@@ -3493,14 +3494,17 @@ Model.bulkWrite = function(ops, options, callback) {
34933494
const validations = ops.map(op => castBulkWrite(this, op, options));
34943495

34953496
callback = this.$handleCallbackError(callback);
3496-
34973497
return promiseOrCallback(callback, cb => {
34983498
cb = this.$wrapCallback(cb);
34993499
each(validations, (fn, cb) => fn(cb), error => {
35003500
if (error) {
35013501
return cb(error);
35023502
}
35033503

3504+
if (ops.length === 0) {
3505+
return cb(null, getDefaultBulkwriteResult());
3506+
}
3507+
35043508
this.collection.bulkWrite(ops, options, (error, res) => {
35053509
if (error) {
35063510
return cb(error);

test/model.test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6799,10 +6799,44 @@ describe('Model', function() {
67996799
assert.equal(typeof users[0].updatedAt, 'number');
68006800
assert.equal(typeof users[1].updatedAt, 'number');
68016801

6802-
// not-lean queries casts to number even if stored on DB as a date
6802+
// not-lean queries cast to number even if stored on DB as a date
68036803
assert.equal(users[0] instanceof User, false);
68046804
assert.equal(users[1] instanceof User, false);
68056805
});
68066806
});
68076807

6808+
it('Model#bulkWrite(...) does not throw an error when provided an empty array (gh-9131)', function() {
6809+
return co(function*() {
6810+
const userSchema = new Schema();
6811+
const User = db.model('User', userSchema);
6812+
6813+
const res = yield User.bulkWrite([]);
6814+
6815+
assert.deepEqual(
6816+
res,
6817+
{
6818+
result: {
6819+
ok: 1,
6820+
writeErrors: [],
6821+
writeConcernErrors: [],
6822+
insertedIds: [],
6823+
nInserted: 0,
6824+
nUpserted: 0,
6825+
nMatched: 0,
6826+
nModified: 0,
6827+
nRemoved: 0,
6828+
upserted: []
6829+
},
6830+
insertedCount: 0,
6831+
matchedCount: 0,
6832+
modifiedCount: 0,
6833+
deletedCount: 0,
6834+
upsertedCount: 0,
6835+
upsertedIds: {},
6836+
insertedIds: {},
6837+
n: 0
6838+
}
6839+
);
6840+
});
6841+
});
68086842
});

0 commit comments

Comments
 (0)