Skip to content

Commit 69e5254

Browse files
committed
fix(retryWrites): fixes more bulk ops to not use retryWrites
bulk updates and deletes cannot retry writes. Fixes NODE-1534
1 parent 124ab31 commit 69e5254

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lib/operations/collection_ops.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,11 @@ function removeDocuments(coll, selector, options, callback) {
11251125

11261126
// Build the op
11271127
const op = { q: selector, limit: 0 };
1128-
if (options.single) op.limit = 1;
1128+
if (options.single) {
1129+
op.limit = 1;
1130+
} else if (finalOptions.retryWrites) {
1131+
finalOptions.retryWrites = false;
1132+
}
11291133

11301134
// Have we specified collation
11311135
decorateWithCollation(finalOptions, coll, options);
@@ -1329,6 +1333,10 @@ function updateDocuments(coll, selector, document, options, callback) {
13291333
delete finalOptions.arrayFilters;
13301334
}
13311335

1336+
if (finalOptions.retryWrites && op.multi) {
1337+
finalOptions.retryWrites = false;
1338+
}
1339+
13321340
// Have we specified collation
13331341
decorateWithCollation(finalOptions, coll, options);
13341342

test/functional/collection_tests.js

+58
Original file line numberDiff line numberDiff line change
@@ -1580,4 +1580,62 @@ describe('Collection', function() {
15801580
});
15811581
}
15821582
});
1583+
1584+
describe('Retryable Writes on bulk ops', function() {
1585+
const MongoClient = require('../../lib/mongo_client');
1586+
1587+
let client;
1588+
let db;
1589+
let collection;
1590+
1591+
const metadata = { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } };
1592+
1593+
beforeEach(function() {
1594+
client = new MongoClient(this.configuration.url(), { retryWrites: true });
1595+
return client.connect().then(() => {
1596+
db = client.db('test_retry_writes');
1597+
collection = db.collection('tests');
1598+
1599+
return Promise.resolve()
1600+
.then(() => db.dropDatabase())
1601+
.then(() => collection.insert({ name: 'foobar' }));
1602+
});
1603+
});
1604+
1605+
afterEach(function() {
1606+
return client.close();
1607+
});
1608+
1609+
it('should succeed with retryWrite=true when using updateMany', {
1610+
metadata,
1611+
test: function() {
1612+
return collection.updateMany({ name: 'foobar' }, { $set: { name: 'fizzbuzz' } });
1613+
}
1614+
});
1615+
1616+
it('should succeed with retryWrite=true when using update with multi=true', {
1617+
metadata,
1618+
test: function() {
1619+
return collection.update(
1620+
{ name: 'foobar' },
1621+
{ $set: { name: 'fizzbuzz' } },
1622+
{ multi: true }
1623+
);
1624+
}
1625+
});
1626+
1627+
it('should succeed with retryWrite=true when using remove without option single', {
1628+
metadata,
1629+
test: function() {
1630+
return collection.remove({ name: 'foobar' });
1631+
}
1632+
});
1633+
1634+
it('should succeed with retryWrite=true when using deleteMany', {
1635+
metadata,
1636+
test: function() {
1637+
return collection.deleteMany({ name: 'foobar' });
1638+
}
1639+
});
1640+
});
15831641
});

0 commit comments

Comments
 (0)