Skip to content

Commit

Permalink
fix(collection): ensure findAndModify always use readPreference primary
Browse files Browse the repository at this point in the history
Fixes NODE-1541
  • Loading branch information
kvwalker authored and mbroadst committed Jul 19, 2018
1 parent c25c519 commit 86344f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/operations/collection_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ function findAndModify(coll, query, sort, doc, options, callback) {
queryObject.bypassDocumentValidation = finalOptions.bypassDocumentValidation;
}

finalOptions.readPreference = ReadPreference.primary;

// Have we specified collation
decorateWithCollation(queryObject, coll, finalOptions);

Expand Down
28 changes: 28 additions & 0 deletions test/functional/find_and_modify_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var f = require('util').format;
var test = require('./shared').assert;
var setupDatabase = require('./shared').setupDatabase;
const expect = require('chai').expect;

describe('Find and Modify', function() {
before(function() {
Expand Down Expand Up @@ -202,4 +203,31 @@ describe('Find and Modify', function() {
});
}
});

it('should allow all findAndModify commands with non-primary readPreference', {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: {
requires: { topology: 'replicaset' }
},

// The actual test we wish to run
test: function(done) {
const configuration = this.configuration;
const client = configuration.newClient({ readPreference: 'secondary' }, { poolSize: 1 });
client.connect((err, client) => {
const db = client.db(configuration.db);
expect(err).to.be.null;

const collection = db.collection('findAndModifyTEST');
// Execute findOneAndUpdate
collection.findOneAndUpdate({}, { $set: { a: 1 } }, err => {
expect(err).to.be.null;

client.close();
done();
});
});
}
});
});

0 comments on commit 86344f4

Please sign in to comment.