Skip to content

Commit

Permalink
fix: unordered bulk write should attempt to execute all batches
Browse files Browse the repository at this point in the history
When an unordered bulk operation is executed, it should attempt to
execute all batches and only return errors after all batches have
been attempted

NODE-2619
  • Loading branch information
mbroadst committed May 26, 2020
1 parent 4faf9f5 commit 6cee96b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/bulk/unordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class UnorderedBulkOperation extends BulkOperationBase {

super(topology, collection, options, false);
}

handleWriteError(callback, writeResult) {
if (this.s.batches.length) {
return false;
}

return super.handleWriteError(callback, writeResult);
}
}

/**
Expand Down
72 changes: 69 additions & 3 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,9 +1637,9 @@ describe('Bulk', function() {
coll.bulkWrite(
[
{ insertOne: { _id: 5, a: 0 } },
{ updateOne: { filter: { _id: 1 }, update: { $set: { a: 0 } } } },
{ updateOne: { filter: { _id: 1 }, update: { $set: { a: 15 } } } },
{ insertOne: { _id: 6, a: 0 } },
{ updateOne: { filter: { _id: 2 }, update: { $set: { a: 0 } } } }
{ updateOne: { filter: { _id: 2 }, update: { $set: { a: 42 } } } }
],
{ ordered: false }
)
Expand All @@ -1666,7 +1666,7 @@ describe('Bulk', function() {
return client.connect().then(() => {
this.defer(() => client.close());

const coll = client.db().collection('bulk_op_ordering_test');
const coll = client.db().collection('unordered_preserve_order');
function ignoreNsNotFound(err) {
if (!err.message.match(/ns not found/)) throw err;
}
Expand Down Expand Up @@ -1697,4 +1697,70 @@ describe('Bulk', function() {
);
});
});

it('should not fail on the first error in an unorderd bulkWrite', function() {
const client = this.configuration.newClient();
return client.connect().then(() => {
this.defer(() => client.close());

const coll = client.db().collection('bulk_op_ordering_test');
function ignoreNsNotFound(err) {
if (!err.message.match(/ns not found/)) throw err;
}

return coll
.drop()
.catch(ignoreNsNotFound)
.then(() => coll.createIndex({ email: 1 }, { unique: 1, background: false }))
.then(() =>
Promise.all([
coll.updateOne(
{ email: 'adam@gmail.com' },
{ $set: { name: 'Adam Smith', age: 29 } },
{ upsert: true }
),
coll.updateOne(
{ email: 'john@gmail.com' },
{ $set: { name: 'John Doe', age: 32 } },
{ upsert: true }
)
])
)
.then(() =>
coll.bulkWrite(
[
{
updateOne: {
filter: { email: 'adam@gmail.com' },
update: { $set: { age: 39 } }
}
},
{
insertOne: {
document: {
email: 'john@gmail.com'
}
}
}
],
{ ordered: false }
)
)
.then(
() => {
throw new Error('expected a bulk error');
},
err =>
expect(err)
.property('code')
.to.equal(11000)
)
.then(() => coll.findOne({ email: 'adam@gmail.com' }))
.then(updatedAdam =>
expect(updatedAdam)
.property('age')
.to.equal(39)
);
});
});
});

0 comments on commit 6cee96b

Please sign in to comment.