From 7b719cdfc14ad9125dd41c240f9aa32315975f55 Mon Sep 17 00:00:00 2001 From: Zach Azar Date: Mon, 18 Nov 2019 10:26:38 -0800 Subject: [PATCH 1/3] Use default db if provided in connection string --- lib/connection.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index a8e418f49a9..edb573a5713 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -605,7 +605,13 @@ Connection.prototype.openUri = function(uri, options, callback) { if (err) { return reject(err); } - this.name = dbName != null ? dbName : get(parsed, 'auth.db', null); + if (dbName) { + this.name = dbName; + } else if (parsed.defaultDatabase) { + this.name = parsed.defaultDatabase; + } else { + this.name = get(parsed, 'auth.db', null); + } this.host = get(parsed, 'hosts.0.host', 'localhost'); this.port = get(parsed, 'hosts.0.port', 27017); this.user = this.user || get(parsed, 'auth.username'); From 09e076631c2f7e3d2c1f5643e2f67bc0fb4be5fc Mon Sep 17 00:00:00 2001 From: Zach Azar Date: Mon, 18 Nov 2019 10:29:30 -0800 Subject: [PATCH 2/3] Add test for default db in uri --- test/connection.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/connection.test.js b/test/connection.test.js index 16b05ac3305..b8cf36d5413 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -744,6 +744,13 @@ describe('connections:', function() { }); }); + it('uses default database in uri if options.dbName is not provided', function() { + return mongoose.createConnection('mongodb://localhost:27017/default-db-name').then(db => { + assert.equal(db.name, 'default-db-name'); + db.close(); + }); + }); + it('startSession() (gh-6653)', function() { const conn = mongoose.createConnection('mongodb://localhost:27017/test'); From 8155c499d4a202451aa8cddf52f9079b781df4dc Mon Sep 17 00:00:00 2001 From: Zach Azar Date: Mon, 18 Nov 2019 10:32:57 -0800 Subject: [PATCH 3/3] Change docs for dbName so that it's not as strict --- docs/connections.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/connections.pug b/docs/connections.pug index 7a4e6bd7e97..4246f7e967e 100644 --- a/docs/connections.pug +++ b/docs/connections.pug @@ -134,7 +134,7 @@ block content * `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) * `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. * `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. - * `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. + * `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). Below are some of the options that are important for tuning mongoose.