diff --git a/src/connection_string.ts b/src/connection_string.ts index ed897000dc..4fc5d817e0 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -291,11 +291,6 @@ export function parseOptions( } } - if (urlOptions.has('authSource')) { - // If authSource is an explicit key in the urlOptions we need to remove the dbName - urlOptions.delete('dbName'); - } - const objectOptions = new CaseInsensitiveMap( Object.entries(options).filter(([, v]) => v != null) ); diff --git a/test/unit/connection_string.test.js b/test/unit/connection_string.test.js index e17276e9c2..f96089a409 100644 --- a/test/unit/connection_string.test.js +++ b/test/unit/connection_string.test.js @@ -99,6 +99,17 @@ describe('Connection String', function () { expect(options.credentials.source).to.equal('0001'); }); + it('should not remove dbName from the options if authSource is provided', function () { + const dbName = 'my-db-name'; + const authSource = 'admin'; + const options = parseOptions( + `mongodb://myName:myPassword@localhost:27017/${dbName}?authSource=${authSource}` + ); + + expect(options).has.property('dbName', dbName); + expect(options.credentials).to.have.property('source', authSource); + }); + it('should parse a replicaSet with a leading number', function () { const options = parseOptions('mongodb://localhost/?replicaSet=123abc'); expect(options).to.have.property('replicaSet');