Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mongodb

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?

like image 502
Ryan Avatar asked Dec 28 '25 14:12

Ryan


1 Answers

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");
like image 65
AdaTheDev Avatar answered Dec 31 '25 17:12

AdaTheDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!