Skip to content

Commit b657c8c

Browse files
authored
fix: respect readPreference and writeConcern from connection string (#2711)
NODE-2965
1 parent b5e9d67 commit b657c8c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

lib/operations/connect.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ function connect(mongoClient, url, options, callback) {
308308
// Store the merged options object
309309
mongoClient.s.options = _finalOptions;
310310

311+
// Apply read and write concern from parsed url
312+
mongoClient.s.readPreference = ReadPreference.fromOptions(_finalOptions);
313+
mongoClient.s.writeConcern = WriteConcern.fromOptions(_finalOptions);
314+
311315
// Failure modes
312316
if (object.servers.length === 0) {
313317
return callback(new Error('connection string must contain at least one seed host'));

test/functional/readpreference.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ const Topology = require('../../lib/core/sdam/topology').Topology;
44
const test = require('./shared').assert;
55
const setupDatabase = require('./shared').setupDatabase;
66
const withMonitoredClient = require('./shared').withMonitoredClient;
7-
const expect = require('chai').expect;
7+
88
const ReadPreference = require('../../lib/core/topologies/read_preference');
99
const withClient = require('./shared').withClient;
10+
const chai = require('chai');
11+
chai.use(require('chai-subset'));
12+
const expect = chai.expect;
1013

1114
describe('ReadPreference', function() {
1215
before(function() {
@@ -729,4 +732,31 @@ describe('ReadPreference', function() {
729732
});
730733
});
731734
});
735+
736+
it('should respect readPreference from uri', {
737+
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
738+
test: withMonitoredClient('find', { queryOptions: { readPreference: 'secondary' } }, function(
739+
client,
740+
events,
741+
done
742+
) {
743+
expect(client.readPreference.mode).to.equal('secondary');
744+
client
745+
.db('test')
746+
.collection('test')
747+
.findOne({ a: 1 }, err => {
748+
expect(err).to.not.exist;
749+
expect(events)
750+
.to.be.an('array')
751+
.with.lengthOf(1);
752+
expect(events[0]).to.containSubset({
753+
commandName: 'find',
754+
command: {
755+
$readPreference: { mode: 'secondary' }
756+
}
757+
});
758+
done();
759+
});
760+
})
761+
});
732762
});

test/functional/write_concern.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ describe('Write Concern', function() {
2121
generateTopologyTests(testSuites, testContext);
2222
});
2323

24+
it(
25+
'should respect writeConcern from uri',
26+
withMonitoredClient('insert', { queryOptions: { w: 0 } }, function(client, events, done) {
27+
expect(client.writeConcern).to.eql({ w: 0 });
28+
client
29+
.db('test')
30+
.collection('test')
31+
.insertOne({ a: 1 }, (err, result) => {
32+
expect(err).to.not.exist;
33+
expect(result).to.exist;
34+
expect(events)
35+
.to.be.an('array')
36+
.with.lengthOf(1);
37+
expect(events[0]).to.containSubset({
38+
commandName: 'insert',
39+
command: {
40+
writeConcern: { w: 0 }
41+
}
42+
});
43+
done();
44+
});
45+
})
46+
);
47+
2448
// TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed
2549
describe('test journal connection string option', function() {
2650
function journalOptionTest(client, events, done) {

0 commit comments

Comments
 (0)