Skip to content

Commit

Permalink
fix(retryWrites): fixes more bulk ops to not use retryWrites
Browse files Browse the repository at this point in the history
bulk updates and deletes cannot retry writes.

Fixes NODE-1534
  • Loading branch information
daprahamian committed Jun 26, 2018
1 parent 124ab31 commit 69e5254
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/operations/collection_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
58 changes: 58 additions & 0 deletions test/functional/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
});
});
});

0 comments on commit 69e5254

Please sign in to comment.