[GODRIVER-914] Configure Database Name From Mongo URI Created: 01/Apr/19  Updated: 06/Aug/20  Resolved: 30/May/19

Status: Closed
Project: Go Driver
Component/s: Connections
Affects Version/s: 1.0.0
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Ross Henderson [X] Assignee: Scott L'Hommedieu (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to DRIVERS-662 Add a URI param and supporting API to... Closed
Case:

 Description   

When using `ClientOptions.ApplyURI` the Database is extracted from the uri inside the `parser.parse` method in `connstring.go`,  but is then not set on `ClientOptions` (in fact this struct does not have a `Database` parameter) and is not passed through to the `Client` at all.

 

Coming from `globalsign/mgo` - we are using `session.DB("")` to allow use of the `defaultDb` inside `session.go` and I would like to see this or something similar implemented for your driver in the `Client.Database` method, as our database name is environment specific and it is far simpler for us to set this value in the mongo uri (one place) than to set it at an application level per environment.

 

I am happy to create a PR to cover this feature and collaborate on this feature going forward, as we are very keen to migrate our applications away from mgo to the official driver, and this small fix would save a lot of configuration overhead for us.



 Comments   
Comment by Scott L'Hommedieu (Inactive) [ 03/Jun/19 ]

As I mentioned, track this in DRIVERS-662.

Comment by Scott L'Hommedieu (Inactive) [ 30/May/19 ]

Thanks for reporting this discrepancy between the mgo driver and the Mongodb Go Driver.  This particular issue isn't something that is described in the Driver Specs and isn't done in any other of the supported MongoDB Drivers.

The connection string spec currently dictates that the path parameter that you're referring to is the auth database.  To prevent confusion of auth database vs default database we can't support this api.

There is potential to add a named parameter to the URI to support default db but that wouldn't be useful without other changes to the "use" that default in the rest of the client api.
I don't see any of that easing your migration burden.

I'll open a Drivers ticket to further debate the merits of adding this behavior to the Drivers specs.

Comment by Scott L'Hommedieu (Inactive) [ 30/May/19 ]

This should be handled as described in DRIVERS-637GODRIVER-1021 tracks that work.

Comment by David Golden [ 01/Apr/19 ]

this brings up the larger question of why the URI allows us to configure the authSource in this way

The (admittedly not very satisfying) answer is that it was a legacy design decision that has persisted for backwards-compatibility reasons.

Thanks for raising this ticket – we will give this some thought. At the very least, we need better documentation for users migrating from mgo.

Comment by Ross Henderson [X] [ 01/Apr/19 ]

Thanks for your response David, I was not aware of this, largely because it seems confusing to provide the database in the uri as a default authSource when you can also specify }}it as a connection string parameter], which would override the value anyway (stopping us from using {{opt.Auth.AuthSource as above).

We are using a uri like this:

mongodb://my-service:pass12345@shard-01:27017,shard-02:27017,shard-03:27017/my-service?ssl=true&authSource=admin

I believe that we can solve our use case for now by configuring our client with a uri like so:

mongodb://my-service:pass12345@shard-01:27017,shard-02:27017,shard-03:27017/?ssl=true

and then using client.Database("my-service") to configure the database to use.

 

Although this brings up the larger question of why the URI allows us to configure the authSource in this way, I think it means we can discard the idea of implementing this feature for the current driver, as using this parameter for a default database does not seem correct to me now that I have taken a closer look at the documentation you've provided. Perhaps a clarification could be made to the documentation somewhere, as this seems somewhat of a gotcha for users that are migrating from mgo, but I cannot see a good place to document this myself.

 

If you have a suggestion of what could be improved I'd welcome it, but otherwise I'm happy for you to close this ticket, and thanks again for your time in helping me resolve this.

 

 

Comment by David Golden [ 01/Apr/19 ]

Before considering the merits of your request, I want mention that the 'database' string in that part of the URI is intended and documented to represent the authentication database, not a "default" database (though some tools/libraries do treat it that way)  It's not well documented currently, but because it's an alternative for the authSource URI parameter, you can access it via the ClientOptions.Auth.AuthSource field:

	opt := options.Client().ApplyURI("mongodb://localhost/foo")
	fmt.Println(opt.Auth.AuthSource) // "foo"

Generated at Thu Feb 08 08:35:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.