Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit be01ffe

Browse files
committed
fix(uri_parser): support a default database on mongodb+srv uris
NODE-1321
1 parent bd8bec4 commit be01ffe

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/uri_parser.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,19 @@ function parseSrvConnectionString(uri, options, callback) {
7171
i === 0 ? `${base}${address.name}:${address.port}` : `${address.name}:${address.port}`
7272
);
7373

74-
let connectionString = connectionStrings.join(',') + '/';
74+
let connectionString = `${connectionStrings.join(',')}/`;
7575
let connectionStringOptions = [];
7676

77+
// Add the default database if needed
78+
if (result.path) {
79+
let defaultDb = result.path.slice(1);
80+
if (defaultDb.indexOf('?') !== -1) {
81+
defaultDb = defaultDb.slice(0, defaultDb.indexOf('?'));
82+
}
83+
84+
connectionString += defaultDb;
85+
}
86+
7787
// Default to SSL true
7888
if (!options.ssl && (!result.search || result.query['ssl'] == null)) {
7989
connectionStringOptions.push('ssl=true');

test/tests/unit/mongodb_srv_spec_tests.js

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const parseConnectionString = require('../../../lib/uri_parser');
55
const expect = require('chai').expect;
66

77
describe('mongodb+srv (spec)', function() {
8+
it('should parse a default database', function(done) {
9+
parseConnectionString('mongodb+srv://test5.test.build.10gen.cc/somedb', (err, result) => {
10+
expect(result.auth.db).to.eql('somedb');
11+
done();
12+
});
13+
});
14+
815
const specPath = path.join(__dirname, '../spec', 'initial-dns-seedlist-discovery');
916
const testFiles = fs
1017
.readdirSync(specPath)

0 commit comments

Comments
 (0)