Skip to content

Commit 08ee53e

Browse files
committed
fix(bulk): use operation index from input to report operation error
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
1 parent daaef89 commit 08ee53e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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.originalZeroIndex + result.writeErrors[i].index,
478+
index: batch.originalIndexes[i],
479479
code: result.writeErrors[i].code,
480480
errmsg: result.writeErrors[i].errmsg,
481481
op: batch.operations[result.writeErrors[i].index]

test/functional/bulk_tests.js

+45
Original file line numberDiff line numberDiff line change
@@ -1615,4 +1615,49 @@ describe('Bulk', function() {
16151615
})
16161616
.then(() => client.close());
16171617
});
1618+
1619+
it('should preserve order of operation index in unordered bulkWrite', function() {
1620+
const client = this.configuration.newClient();
1621+
return client.connect().then(() => {
1622+
this.defer(() => client.close());
1623+
1624+
const coll = client.db().collection('bulk_write_ordering_test');
1625+
function ignoreNsNotFound(err) {
1626+
if (!err.message.match(/ns not found/)) throw err;
1627+
}
1628+
1629+
return coll
1630+
.drop()
1631+
.catch(ignoreNsNotFound)
1632+
.then(() => coll.insert(Array.from({ length: 4 }, (_, i) => ({ _id: i, a: i }))))
1633+
.then(() =>
1634+
coll
1635+
.createIndex({ a: 1 }, { unique: true })
1636+
.then(() =>
1637+
coll.bulkWrite(
1638+
[
1639+
{ insertOne: { _id: 5, a: 0 } },
1640+
{ updateOne: { filter: { _id: 1 }, update: { $set: { a: 0 } } } },
1641+
{ insertOne: { _id: 6, a: 0 } },
1642+
{ updateOne: { filter: { _id: 2 }, update: { $set: { a: 0 } } } }
1643+
],
1644+
{ ordered: false }
1645+
)
1646+
)
1647+
)
1648+
.then(
1649+
() => {
1650+
throw new Error('expected a bulk error');
1651+
},
1652+
err => {
1653+
expect(err)
1654+
.to.have.property('writeErrors')
1655+
.with.length(2);
1656+
1657+
expect(err).to.have.nested.property('writeErrors[0].err.index', 0);
1658+
expect(err).to.have.nested.property('writeErrors[1].err.index', 2);
1659+
}
1660+
);
1661+
});
1662+
});
16181663
});

0 commit comments

Comments
 (0)