Skip to content

Commit e366bca

Browse files
authored
Merge pull request #8355 from BuildingConnected/feat/use-default-db-in-connection-string
feat(connections): Adds default database connection string support (#8354)
2 parents 3e9faef + 8155c49 commit e366bca

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

docs/connections.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ block content
134134
* `bufferCommands` - This is a mongoose-specific option (not passed to the MongoDB driver) that disables [mongoose's buffering mechanism](http://mongoosejs.com/docs/faq.html#callback_never_executes)
135135
* `user`/`pass` - The username and password for authentication. These options are mongoose-specific, they are equivalent to the MongoDB driver's `auth.user` and `auth.password` options.
136136
* `autoIndex` - By default, mongoose will automatically build indexes defined in your schema when it connects. This is great for development, but not ideal for large production deployments, because index builds can cause performance degradation. If you set `autoIndex` to false, mongoose will not automatically build indexes for **any** model associated with this connection.
137-
* `dbName` - Specifies which database to connect to and overrides any database specified in the connection string. If you're using the `mongodb+srv` syntax to connect to [MongoDB Atlas](https://www.mongodb.com/cloud/atlas), you [should use `dbName` to specify the database](https://stackoverflow.com/questions/48917591/fail-to-connect-mongoose-to-atlas/48917626#48917626) because you currently cannot in the connection string.
137+
* `dbName` - Specifies which database to connect to and overrides any database specified in the connection string. This is useful if you are unable to specify a default database in the connection string like with [some `mongodb+srv` syntax connections](https://stackoverflow.com/questions/48917591/fail-to-connect-mongoose-to-atlas/48917626#48917626).
138138

139139
Below are some of the options that are important for tuning mongoose.
140140

lib/connection.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,13 @@ Connection.prototype.openUri = function(uri, options, callback) {
605605
if (err) {
606606
return reject(err);
607607
}
608-
this.name = dbName != null ? dbName : get(parsed, 'auth.db', null);
608+
if (dbName) {
609+
this.name = dbName;
610+
} else if (parsed.defaultDatabase) {
611+
this.name = parsed.defaultDatabase;
612+
} else {
613+
this.name = get(parsed, 'auth.db', null);
614+
}
609615
this.host = get(parsed, 'hosts.0.host', 'localhost');
610616
this.port = get(parsed, 'hosts.0.port', 27017);
611617
this.user = this.user || get(parsed, 'auth.username');

test/connection.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,13 @@ describe('connections:', function() {
744744
});
745745
});
746746

747+
it('uses default database in uri if options.dbName is not provided', function() {
748+
return mongoose.createConnection('mongodb://localhost:27017/default-db-name').then(db => {
749+
assert.equal(db.name, 'default-db-name');
750+
db.close();
751+
});
752+
});
753+
747754
it('startSession() (gh-6653)', function() {
748755
const conn = mongoose.createConnection('mongodb://localhost:27017/test');
749756

0 commit comments

Comments
 (0)