Skip to content

Commit

Permalink
fix(bulk): use original indexes as map for current op index
Browse files Browse the repository at this point in the history
The fix for bulk introduced in v3.4.0 did not take into account
the index of the operation result when referring to the original
stored index map.

NODE-2383
  • Loading branch information
mbroadst committed Dec 16, 2019
1 parent 3f34e3e commit 20800ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bulk/common.js
Original file line number Diff line number Diff line change
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.originalIndexes[i],
index: batch.originalIndexes[result.writeErrors[i].index],
code: result.writeErrors[i].code,
errmsg: result.writeErrors[i].errmsg,
op: batch.operations[result.writeErrors[i].index]
Expand Down
37 changes: 37 additions & 0 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1660,4 +1660,41 @@ describe('Bulk', function() {
);
});
});

it('should preserve order of operation index in unordered bulk operation', 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(() => {
const batch = coll.initializeUnorderedBulkOp();
batch.insert({ _id: 1, a: 0 });
batch.insert({ _id: 1, a: 0 });
batch.insert({ _id: 2, a: 0 });
batch.insert({ _id: 2, a: 0 });
return batch.execute();
})
.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', 1);
expect(err).to.have.nested.property('writeErrors[1].err.index', 3);
}
);
});
});
});

0 comments on commit 20800ac

Please sign in to comment.