3

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?

Ryan
  • 645
  • 4
  • 11

1 Answers1

2

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");
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
  • That's what I'm looking for. I didn't realize I could just create the Database object without first going though the MongoServer object. Thanks! – Ryan Nov 10 '11 at 21:37
  • 1
    Hmm, MongoDatabase.Create is now marked as obsolete. I wonder what the replacement for this scenario is? – Richard Fawcett Jan 31 '13 at 13:55
  • 2
    This answer shows a way to do it without using the obsolete MongoDatabase.Create method. http://stackoverflow.com/a/7202105/431891 In short, you use MongoUrl.Create to parse a MongoUrl object from the connection string, and use its DatabaseName property. – Richard Fawcett Jan 31 '13 at 14:03