From 86344f4e9aed59e6ecf1efacd374246aac120eaf Mon Sep 17 00:00:00 2001 From: Katherine Walker Date: Thu, 19 Jul 2018 13:00:30 -0400 Subject: [PATCH] fix(collection): ensure findAndModify always use readPreference primary Fixes NODE-1541 --- lib/operations/collection_ops.js | 2 ++ test/functional/find_and_modify_tests.js | 28 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/operations/collection_ops.js b/lib/operations/collection_ops.js index 7d43d19dc8..f9ddfb8af0 100644 --- a/lib/operations/collection_ops.js +++ b/lib/operations/collection_ops.js @@ -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); diff --git a/test/functional/find_and_modify_tests.js b/test/functional/find_and_modify_tests.js index 377d9a36c3..442b644043 100644 --- a/test/functional/find_and_modify_tests.js +++ b/test/functional/find_and_modify_tests.js @@ -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() { @@ -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(); + }); + }); + } + }); });