Skip to content

Commit 86344f4

Browse files
kvwalkermbroadst
authored andcommittedJul 19, 2018
fix(collection): ensure findAndModify always use readPreference primary
Fixes NODE-1541
1 parent c25c519 commit 86344f4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎lib/operations/collection_ops.js

+2
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ function findAndModify(coll, query, sort, doc, options, callback) {
491491
queryObject.bypassDocumentValidation = finalOptions.bypassDocumentValidation;
492492
}
493493

494+
finalOptions.readPreference = ReadPreference.primary;
495+
494496
// Have we specified collation
495497
decorateWithCollation(queryObject, coll, finalOptions);
496498

‎test/functional/find_and_modify_tests.js

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var f = require('util').format;
33
var test = require('./shared').assert;
44
var setupDatabase = require('./shared').setupDatabase;
5+
const expect = require('chai').expect;
56

67
describe('Find and Modify', function() {
78
before(function() {
@@ -202,4 +203,31 @@ describe('Find and Modify', function() {
202203
});
203204
}
204205
});
206+
207+
it('should allow all findAndModify commands with non-primary readPreference', {
208+
// Add a tag that our runner can trigger on
209+
// in this case we are setting that node needs to be higher than 0.10.X to run
210+
metadata: {
211+
requires: { topology: 'replicaset' }
212+
},
213+
214+
// The actual test we wish to run
215+
test: function(done) {
216+
const configuration = this.configuration;
217+
const client = configuration.newClient({ readPreference: 'secondary' }, { poolSize: 1 });
218+
client.connect((err, client) => {
219+
const db = client.db(configuration.db);
220+
expect(err).to.be.null;
221+
222+
const collection = db.collection('findAndModifyTEST');
223+
// Execute findOneAndUpdate
224+
collection.findOneAndUpdate({}, { $set: { a: 1 } }, err => {
225+
expect(err).to.be.null;
226+
227+
client.close();
228+
done();
229+
});
230+
});
231+
}
232+
});
205233
});

0 commit comments

Comments
 (0)
Please sign in to comment.