89

I would like to connect to the database specified in the connection string, without specifying it again in GetDatabase.

For example, if I have a connection string like this;

mongodb://localhost/mydb

I would like to be able to db.GetCollection("mycollection") from mydb.

This would allow the database name to be configured easily in the app.config file.

4 Answers 4

97

Update:

MongoServer.Create is obsolete now (thanks to @aknuds1). Instead this use following code:

var _server = new MongoClient(connectionString).GetServer();

It's easy. You should first take database name from connection string and then get database by name. Complete example:

var connectionString = "mongodb://localhost:27020/mydb";

//take database name from connection string
var _databaseName = MongoUrl.Create(connectionString).DatabaseName;
var _server = MongoServer.Create(connectionString);

//and then get database by database name:
_server.GetDatabase(_databaseName);

Important: If your database and auth database are different, you can add a authSource= query parameter to specify a different auth database. (thank you to @chrisdrobison)

From docs:

NOTE If you are using the database segment as the initial database to use, but the username and password specified are defined in a different database, you can use the authSource option to specify the database in which the credential is defined. For example, mongodb://user:pass@hostname/db1?authSource=userDb would authenticate the credential against the userDb database instead of db1.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, ok - I see my problem. I don't have access to the connection string, but only the already-created MongoServer object. And it doesn't appear to know about the connection string-specified database. Thanks.
MongoServer.Create is obsolete.
Deleted my last comment since it wasn't quite correct. One thing to note though is that according to the Mongo docs, that it also telling the driver which database you want to auth against. If your database and auth database are different, you can add a authSource= query parameter to specify a different auth database.
@chrisdrobison Thank you! This important to know (updated my answer)! The question was answered 3.5 years ago. Things changed :)
32

In this moment with the last version of the C# driver (2.3.0) the only way I found to get the database name specified in connection string is this:

var connectionString = @"mongodb://usr:[email protected],srv2.acme.net,srv3.acme.net/dbName?replicaSet=rset";
var mongoUrl = new MongoUrl(connectionString);
var dbname = mongoUrl.DatabaseName;
var db = new MongoClient(mongoUrl).GetDatabase(dbname);
db.GetCollection<MyType>("myCollectionName");

1 Comment

This should be upvoted, altough I know this is an old thread. This is the only way to get this in Version 2.3. Thanks for the answer, it took some searching to get here.
10

With version 1.7 of the official 10gen driver, this is the current (non-obsolete) API:

const string uri = "mongodb://localhost/mydb";
var client = new MongoClient(uri);
var db = client.GetServer().GetDatabase(new MongoUrl(uri).DatabaseName);
var collection = db.GetCollection("mycollection");

1 Comment

fwif: This also works in Xamarin Studio 5.8.1 using the mongo driver 1.10
5

The answer below is apparently obsolete now, but works with older drivers. See comments.

If you have the connection string you could also use MongoDatabase directly:

var db =  MongoDatabase.Create(connectionString);
var coll = db.GetCollection("MyCollection");

2 Comments

As time passed, MongoDatabase.Create() has been obsoleted since driver version 1.7
@runTarm But time goes on, and the specification of database names in C# mongo connection strings is not getting obsoleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.