Skip to content

Commit 6d39b2a

Browse files
committed
fix(url_parser): support a default database on mongodb+srv uris
NODE-1321
1 parent 02729dc commit 6d39b2a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/url_parser.js

+10
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ module.exports = function(url, options, callback) {
6363
let connectionString = connectionStrings.join(',') + '/';
6464
let connectionStringOptions = [];
6565

66+
// Add the default database if needed
67+
if (result.path) {
68+
let defaultDb = result.path.slice(1);
69+
if (defaultDb.indexOf('?') !== -1) {
70+
defaultDb = defaultDb.slice(0, defaultDb.indexOf('?'));
71+
}
72+
73+
connectionString += defaultDb;
74+
}
75+
6676
// Default to SSL true
6777
if (!options.ssl && !result.search) {
6878
connectionStringOptions.push('ssl=true');

test/functional/mongodb_srv_tests.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ function getTests() {
1414
.map(x => [path.basename(x[0], '.json'), JSON.parse(x[1])]);
1515
}
1616

17-
describe('DNS and TXT record tests', function() {
17+
describe('mongodb+srv (spec)', function() {
18+
it('should parse a default database', function(done) {
19+
parse('mongodb+srv://test5.test.build.10gen.cc/somedb', (err, result) => {
20+
expect(result.dbName).to.eql('somedb');
21+
done();
22+
});
23+
});
24+
1825
getTests().forEach(function(test) {
1926
if (!test[1].comment) test[1].comment = test[0];
2027

0 commit comments

Comments
 (0)