Amit kumar
Amit kumar

Reputation: 161

Get list of database names

When I tried following

var dbclient = new MongoClient();

var connectionString = "mongodb://127.0.0.1:27017";
dbclient = new MongoClient(connectionString);

// Database List  
var dbList = dbclient.ListDatabases().ToList();

Console.WriteLine("The list of databases are :");

foreach (var item in dbList)
{
    Console.WriteLine(item);

    foreach (var name in item)
    {
        listBox1.Items.Add(name);
    }
}

Output is:

enter image description here

I just need names like "admin", "blog", "config", "local"

Upvotes: 0

Views: 164

Answers (1)

Adrian
Adrian

Reputation: 8597

You have an inner foreach... this prints out all items in the collection.

foreach (var name in item)
{
    listBox1.Items.Add(name);
}

What you want is to remove it and access the name index directly rather than iterating over everything in the collection..

foreach (var item in dbList)
{
    listBox1.Items.Add(item["name"]);
}

Upvotes: 1

Related Questions