Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore auto direct connection behavior #2719

Merged
merged 4 commits into from Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/core/uri_parser.js
Expand Up @@ -687,6 +687,15 @@ function parseConnectionString(uri, options, callback) {
return callback(new MongoParseError('directConnection option requires exactly one host'));
}

// NOTE: this behavior will go away in v4.0, we will always auto discover there
if (
parsedOptions.directConnection == null &&
hosts.length === 1 &&
parsedOptions.replicaSet == null
) {
parsedOptions.directConnection = true;
}

const result = {
hosts: hosts,
auth: auth.db || auth.username ? auth : null,
Expand Down
14 changes: 14 additions & 0 deletions test/functional/mongo_client_options.test.js
Expand Up @@ -168,6 +168,20 @@ describe('MongoClient Options', function() {
}
});

it('should directConnect when no replicaSet name is specified', {
metadata: { requires: { topology: 'replicaset' } },
test: function(done) {
// strip the replicaSet parameter from the url if present
const urlNoReplicaSet = this.configuration.url().replace(/([&?])replicaSet=.+?[&$]/, '$1');
const client = this.configuration.newClient(urlNoReplicaSet);
client.connect(err => {
expect(err).to.not.exist;
expect(client.s.options.directConnection).to.be.true;
client.close(done);
});
}
});

/**
* @ignore
*/
Expand Down
7 changes: 6 additions & 1 deletion test/functional/sharding_connection.test.js
Expand Up @@ -13,7 +13,12 @@ describe('Sharding (Connection)', function() {
it('Should use sharded topology', {
metadata: { requires: { topology: 'sharded' } },
test: function() {
const client = this.configuration.newClient({}, { useUnifiedTopology: true });
const client = this.configuration.newClient(
{},
// note: auto-discovery will be the default behavior in 4.0,
// for now we must supply directConnection: false
{ useUnifiedTopology: true, directConnection: false }
);
return withClient(client, (client, done) => {
expect(client).to.exist;
expect(client)
Expand Down