Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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.

1 Answer

0 votes
by (106k points)

For getting the Mongo database specified in the connection string in C# you can do 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");

Browse Categories

...