Skip to content

Remove the database from the connection string. #971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2016
Merged

Remove the database from the connection string. #971

merged 1 commit into from
Oct 31, 2016

Conversation

philipbrown
Copy link
Contributor

This PR removes the database from the connection string as advised here mongodb/mongo-php-library#171.

If you are expecting to authenticate against the "admin" database, I would suggest removing both "/database" from the URI string and the db key from the options. The driver already defaults to authenticate against the "admin" database. If you do wish to specify a different database, you can either do so via the URI string (where you're currently indicating that you want to auth against the database named "database") or the authSource option (specified as either a query string parameter in the URI string or via the options array).

I realize this does differ from the legacy driver's MongoClient constructor options, where we previously accepted a "db" option. The driver and library documentation for MongoDB\Driver\Manager and MongoDB\Client, respectively, now refer to the Connection string documentation, which I cite below.

/database: Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
Source: Standard Connection String Format.

authSource: Specify the database name associated with the user’s credentials, if the users collection do not exist in the database where the client is connecting. authSource defaults to the database specified in the connection string.

MongoDB will ignore authSource values if the connection string specifies no user name.
Source: Authentication Options.

In your case, you are specifying a username via the array options, so you need not worry about the last line of the authSource docs (if you decide to use that option).

Also potentially related to #963

@jenssegers
Copy link
Contributor

@pi0 Can I get your input on this one? :)

@philipbrown
Copy link
Contributor Author

Hey @pi0, any thoughts on this? ☺️

@@ -183,7 +183,7 @@ protected function getDsn(array $config)
}
}

return "mongodb://" . implode(',', $hosts) . "/{$database}";
return "mongodb://" . implode(',', $hosts);
Copy link
Contributor

@pi0 pi0 Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A agree with your reasons :) but it may break many older projects relying on this type of connection if they just upgrade! So why not using a backward compatible trick like this :

return "mongodb://" . implode(',', $hosts) . ($database? "/{$database}" : '');

@philipbrown
Copy link
Contributor Author

philipbrown commented Oct 28, 2016

@pi0 Great suggestion! I've updated the PR 😄

@jenssegers jenssegers merged commit 6c020a6 into mongodb:master Oct 31, 2016
mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants