From 69e525497de8fe0518b5c79a510c4191eeff6283 Mon Sep 17 00:00:00 2001 From: Dan Aprahamian Date: Tue, 26 Jun 2018 14:05:06 -0400 Subject: [PATCH] fix(retryWrites): fixes more bulk ops to not use retryWrites bulk updates and deletes cannot retry writes. Fixes NODE-1534 --- lib/operations/collection_ops.js | 10 ++++- test/functional/collection_tests.js | 58 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/operations/collection_ops.js b/lib/operations/collection_ops.js index 0d5d3e391d..fa6864ee7c 100644 --- a/lib/operations/collection_ops.js +++ b/lib/operations/collection_ops.js @@ -1125,7 +1125,11 @@ function removeDocuments(coll, selector, options, callback) { // Build the op const op = { q: selector, limit: 0 }; - if (options.single) op.limit = 1; + if (options.single) { + op.limit = 1; + } else if (finalOptions.retryWrites) { + finalOptions.retryWrites = false; + } // Have we specified collation decorateWithCollation(finalOptions, coll, options); @@ -1329,6 +1333,10 @@ function updateDocuments(coll, selector, document, options, callback) { delete finalOptions.arrayFilters; } + if (finalOptions.retryWrites && op.multi) { + finalOptions.retryWrites = false; + } + // Have we specified collation decorateWithCollation(finalOptions, coll, options); diff --git a/test/functional/collection_tests.js b/test/functional/collection_tests.js index b9a38caeff..60699ec921 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -1580,4 +1580,62 @@ describe('Collection', function() { }); } }); + + describe('Retryable Writes on bulk ops', function() { + const MongoClient = require('../../lib/mongo_client'); + + let client; + let db; + let collection; + + const metadata = { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } }; + + beforeEach(function() { + client = new MongoClient(this.configuration.url(), { retryWrites: true }); + return client.connect().then(() => { + db = client.db('test_retry_writes'); + collection = db.collection('tests'); + + return Promise.resolve() + .then(() => db.dropDatabase()) + .then(() => collection.insert({ name: 'foobar' })); + }); + }); + + afterEach(function() { + return client.close(); + }); + + it('should succeed with retryWrite=true when using updateMany', { + metadata, + test: function() { + return collection.updateMany({ name: 'foobar' }, { $set: { name: 'fizzbuzz' } }); + } + }); + + it('should succeed with retryWrite=true when using update with multi=true', { + metadata, + test: function() { + return collection.update( + { name: 'foobar' }, + { $set: { name: 'fizzbuzz' } }, + { multi: true } + ); + } + }); + + it('should succeed with retryWrite=true when using remove without option single', { + metadata, + test: function() { + return collection.remove({ name: 'foobar' }); + } + }); + + it('should succeed with retryWrite=true when using deleteMany', { + metadata, + test: function() { + return collection.deleteMany({ name: 'foobar' }); + } + }); + }); });