Skip to content

Database name should be parsed from connection string when available #8354

@zachazar

Description

@zachazar

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
When you connect to MongoDB, you provide a dbName option to specify which database to connect to. This is handled here

mongoose/lib/connection.js

Lines 604 to 608 in 3e9faef

parseConnectionString(uri, options, (err, parsed) => {
if (err) {
return reject(err);
}
this.name = dbName != null ? dbName : get(parsed, 'auth.db', null);

This was added in #6106 in response to SRV connection strings lacking a database name, and thus the dbName workaround was added.

What is the expected behavior?
Now when testing with Atlas connection strings, it looks like we have the ability to specify a default database in the connection string - and parseConnectionString() successfully parses it.

The spec doesn't show this option, however Atlas uses it and even shows an example of defaulting to a test database in their Driver Examples documentation.

Thus, we believe the logic here should be (in this order):

  1. if dbName is provided in the options, use it
  2. if the database name is provided in the connection string (parsed as defaultDatabase), use it
  3. if neither of the above are provided, default to the authorization database

Currently there is no step 2.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: 10.15
Mongoose: 5.4
MongoDB: 3.6


I'm working on a PR right now to add this logic and update these docs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions