Skip to content

Commit 17c31b7

Browse files
committed
fix(model): make Model.bulkWrite() with empty array and ordered false not throw an error
Fix #13664
1 parent 0229ffd commit 17c31b7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/model.js

+4
Original file line numberDiff line numberDiff line change
@@ -3796,6 +3796,10 @@ Model.bulkWrite = function(ops, options, callback) {
37963796
function completeUnorderedValidation() {
37973797
validOps = validOps.sort().map(index => ops[index]);
37983798

3799+
if (validOps.length === 0) {
3800+
return cb(null, getDefaultBulkwriteResult());
3801+
}
3802+
37993803
this.$__collection.bulkWrite(validOps, options, (error, res) => {
38003804
if (error) {
38013805
if (validationErrors.length > 0) {

test/model.test.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -7856,9 +7856,32 @@ describe('Model', function() {
78567856
const userSchema = new Schema({ name: String });
78577857
const User = db.model('User', userSchema);
78587858

7859-
const err = await User.bulkWrite([], { ordered: false }).then(() => null, err => err);
7860-
assert.ok(err);
7861-
assert.equal(err.name, 'MongoInvalidArgumentError');
7859+
const res = await User.bulkWrite([], { ordered: false });
7860+
assert.deepEqual(
7861+
res,
7862+
{
7863+
result: {
7864+
ok: 1,
7865+
writeErrors: [],
7866+
writeConcernErrors: [],
7867+
insertedIds: [],
7868+
nInserted: 0,
7869+
nUpserted: 0,
7870+
nMatched: 0,
7871+
nModified: 0,
7872+
nRemoved: 0,
7873+
upserted: []
7874+
},
7875+
insertedCount: 0,
7876+
matchedCount: 0,
7877+
modifiedCount: 0,
7878+
deletedCount: 0,
7879+
upsertedCount: 0,
7880+
upsertedIds: {},
7881+
insertedIds: {},
7882+
n: 0
7883+
}
7884+
);
78627885
});
78637886

78647887
it('allows calling `create()` after `bulkWrite()` (gh-9350)', async function() {

0 commit comments

Comments
 (0)