Reputation: 168
I am using MongoDB driver, to communicate with Mongo database. I have a need where I need display all available databases. The library has only on method that says 'GetDatabase("dbname"). What is the way to get all dbs available in ny code.
Upvotes: 0
Views: 617
Reputation: 6322
Here is how you can get the list of dbs
MongoClient client = new MongoClient("mongodb://localhost:27017");
List<string> dbs = new List<string>();
using (IAsyncCursor<BsonDocument> cursor = client.ListDatabases())
{
while (cursor.MoveNext())
{
foreach (var doc in cursor.Current)
dbs.Add((string)doc["name"]); // database name
}
}
Upvotes: 1