Skip to content

Commit 20800ac

Browse files
committed
fix(bulk): use original indexes as map for current op index
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
1 parent 3f34e3e commit 20800ac

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/bulk/common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ function mergeBatchResults(batch, bulkResult, err, result) {
475475
if (Array.isArray(result.writeErrors)) {
476476
for (let i = 0; i < result.writeErrors.length; i++) {
477477
const writeError = {
478-
index: batch.originalIndexes[i],
478+
index: batch.originalIndexes[result.writeErrors[i].index],
479479
code: result.writeErrors[i].code,
480480
errmsg: result.writeErrors[i].errmsg,
481481
op: batch.operations[result.writeErrors[i].index]

test/functional/bulk.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -1660,4 +1660,41 @@ describe('Bulk', function() {
16601660
);
16611661
});
16621662
});
1663+
1664+
it('should preserve order of operation index in unordered bulk operation', function() {
1665+
const client = this.configuration.newClient();
1666+
return client.connect().then(() => {
1667+
this.defer(() => client.close());
1668+
1669+
const coll = client.db().collection('bulk_op_ordering_test');
1670+
function ignoreNsNotFound(err) {
1671+
if (!err.message.match(/ns not found/)) throw err;
1672+
}
1673+
1674+
return coll
1675+
.drop()
1676+
.catch(ignoreNsNotFound)
1677+
.then(() => {
1678+
const batch = coll.initializeUnorderedBulkOp();
1679+
batch.insert({ _id: 1, a: 0 });
1680+
batch.insert({ _id: 1, a: 0 });
1681+
batch.insert({ _id: 2, a: 0 });
1682+
batch.insert({ _id: 2, a: 0 });
1683+
return batch.execute();
1684+
})
1685+
.then(
1686+
() => {
1687+
throw new Error('expected a bulk error');
1688+
},
1689+
err => {
1690+
expect(err)
1691+
.to.have.property('writeErrors')
1692+
.with.length(2);
1693+
1694+
expect(err).to.have.nested.property('writeErrors[0].err.index', 1);
1695+
expect(err).to.have.nested.property('writeErrors[1].err.index', 3);
1696+
}
1697+
);
1698+
});
1699+
});
16631700
});

0 commit comments

Comments
 (0)