Skip to content

Commit

Permalink
fix(bulk): use operation index from input to report operation error
Browse files Browse the repository at this point in the history
The bulk spec requires that the driver report the index of a failed
operation according to it's input to the unordered bulk operation.

NODE-2308
  • Loading branch information
mbroadst committed Nov 25, 2019
1 parent daaef89 commit 08ee53e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bulk/common.js
Expand Up @@ -475,7 +475,7 @@ function mergeBatchResults(batch, bulkResult, err, result) {
if (Array.isArray(result.writeErrors)) {
for (let i = 0; i < result.writeErrors.length; i++) {
const writeError = {
index: batch.originalZeroIndex + result.writeErrors[i].index,
index: batch.originalIndexes[i],
code: result.writeErrors[i].code,
errmsg: result.writeErrors[i].errmsg,
op: batch.operations[result.writeErrors[i].index]
Expand Down
45 changes: 45 additions & 0 deletions test/functional/bulk_tests.js
Expand Up @@ -1615,4 +1615,49 @@ describe('Bulk', function() {
})
.then(() => client.close());
});

it('should preserve order of operation index in unordered bulkWrite', function() {
const client = this.configuration.newClient();
return client.connect().then(() => {
this.defer(() => client.close());

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

return coll
.drop()
.catch(ignoreNsNotFound)
.then(() => coll.insert(Array.from({ length: 4 }, (_, i) => ({ _id: i, a: i }))))
.then(() =>
coll
.createIndex({ a: 1 }, { unique: true })
.then(() =>
coll.bulkWrite(
[
{ insertOne: { _id: 5, a: 0 } },
{ updateOne: { filter: { _id: 1 }, update: { $set: { a: 0 } } } },
{ insertOne: { _id: 6, a: 0 } },
{ updateOne: { filter: { _id: 2 }, update: { $set: { a: 0 } } } }
],
{ ordered: false }
)
)
)
.then(
() => {
throw new Error('expected a bulk error');
},
err => {
expect(err)
.to.have.property('writeErrors')
.with.length(2);

expect(err).to.have.nested.property('writeErrors[0].err.index', 0);
expect(err).to.have.nested.property('writeErrors[1].err.index', 2);
}
);
});
});
});

0 comments on commit 08ee53e

Please sign in to comment.