Ryan
Ryan

Reputation: 655

How do I get the current/default database (MongoDB, official C# driver)?

The C# driver tutorial gives the following format for the connection string (which includes the option to specify a default database):

mongodb://[username:password@]hostname[:port][/[database][?options]]

But I don't see an overload of the GetDatabase method that doesn't require providing the database name. Is there some other method of getting a MongoDatabase instance that represents the database specified in the connection string?

Upvotes: 3

Views: 2923

Answers (1)

AdaTheDev
AdaTheDev

Reputation: 147374

There isn't an overload for that. You could use this approach instead:

var db = MongoDatabase.Create("mongodb://localhost:27017/SomeDatabase");
var collection = db.GetCollection("MyCollection");

Upvotes: 2

Related Questions