Reputation: 148524
I'm instantiating MongoClient
with full connection string which includes DB :
MongoClient dbClient = new MongoClient("mongodb://***:***@***:27017/myDb");
var dbList = dbClient.ListDatabases();
IMongoDatabase db = dbClient.GetDatabase("myDb");
var collList = db.ListCollections().ToList();
...
This works. But -
If the connection string already includes myDb
, then why do I need to write again :
dbClient.GetDatabase("myDb");
?
I've already written myDb
in the connection string.
Question:
Is there any option to ListCollections
of the DB which already mentioned in the connection string?
Upvotes: 1
Views: 170
Reputation: 93063
One option is to just pull out the database name from the connection string. That might sound a bit messy, but MongoUrl
makes it nice and easy. Here's an example:
var connectionString = "...";
var dbName = MongoUrl.Create(connectionString).DatabaseName;
// ...
IMongoDatabase db = dbClient.GetDatabase(dbName);
Upvotes: 1