MongoDB: Getting the list of all databases?

后端 未结 5 873
太阳男子
太阳男子 2021-01-01 22:38

How do I list all databases for a connection using Mongo C# Driver?

5条回答
  •  别那么骄傲
    2021-01-01 23:20

    Working Solution:

    MongoClient client = new MongoClient("mongodb://localhost:27017");
    using (IAsyncCursor cursor = client.ListDatabases())
    {
        while (cursor.MoveNext())
        {
            foreach (var doc in cursor.Current)
            {
                Console.WriteLine(doc["name"]); // database name
            }
        }
    }
    

提交回复
热议问题