Skip to content

Commit

Permalink
fix(model): make Model.bulkWrite() with empty array and ordered false…
Browse files Browse the repository at this point in the history
… not throw an error

Fix #13664
  • Loading branch information
vkarpov15 committed Aug 22, 2023
1 parent 0229ffd commit 17c31b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/model.js
Expand Up @@ -3796,6 +3796,10 @@ Model.bulkWrite = function(ops, options, callback) {
function completeUnorderedValidation() {
validOps = validOps.sort().map(index => ops[index]);

if (validOps.length === 0) {
return cb(null, getDefaultBulkwriteResult());
}

this.$__collection.bulkWrite(validOps, options, (error, res) => {
if (error) {
if (validationErrors.length > 0) {
Expand Down
29 changes: 26 additions & 3 deletions test/model.test.js
Expand Up @@ -7856,9 +7856,32 @@ describe('Model', 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');
const res = await User.bulkWrite([], { ordered: false });
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
}
);
});

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

0 comments on commit 17c31b7

Please sign in to comment.