Skip to content

Commit

Permalink
Merge pull request #13770 from Automattic/vkarpov15/gh-13664-2
Browse files Browse the repository at this point in the history
fix(model): make Model.bulkWrite() with empty array and ordered false not throw an error
  • Loading branch information
vkarpov15 authored Aug 24, 2023
2 parents 0229ffd + 17c31b7 commit 4f79ce2
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 4f79ce2

Please sign in to comment.