MongoDB:获取所有数据库的列表?

发布于 2024-11-09 15:04:19 字数 36 浏览 0 评论 0原文

如何使用 Mongo C# 驱动程序列出连接的所有数据库?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神爱温柔 2024-11-16 15:04:19

很容易:

var server = MongoServer.Create("mongodb://localhost/?safe=true");
var databaseNames = server.GetDatabaseNames();

Very easily:

var server = MongoServer.Create("mongodb://localhost/?safe=true");
var databaseNames = server.GetDatabaseNames();
瞄了个咪的 2024-11-16 15:04:19

MongoServer 类在 2.0.0 版本中已弃用。

您可以使用 ListDatabasesAsync

using (var cursor = await client.ListDatabasesAsync())
{
    await cursor.ForEachAsync(d => Console.WriteLine(d.ToString()));
}

The MongoServer class was deprecated in version 2.0.0.

You can use ListDatabasesAsync

using (var cursor = await client.ListDatabasesAsync())
{
    await cursor.ForEachAsync(d => Console.WriteLine(d.ToString()));
}
梦里梦着梦中梦 2024-11-16 15:04:19

工作解决方案:

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

Working Solution:

MongoClient client = new MongoClient("mongodb://localhost:27017");
using (IAsyncCursor<BsonDocument> cursor = client.ListDatabases())
{
    while (cursor.MoveNext())
    {
        foreach (var doc in cursor.Current)
        {
            Console.WriteLine(doc["name"]); // database name
        }
    }
}
梦罢 2024-11-16 15:04:19

正如 Juri 指出的,MongoServer 类在 2.0.0 版本中已被弃用。如果您不想使用异步,我是这样做的:

var client = new MongoClient("mongodb://" + server_username + ":" + server_password + "@" + server_host + ":" +  server_port);

List<MongoDB.Bson.BsonDocument> databases = client.ListDatabases();

只有一件事。它采用 BsonDocument 格式,有 2 个元素:“name”和“sizeOnDisk”。

希望这有帮助。

The MongoServer class was deprecated in version 2.0.0 as Juri pointed out. If you don't want to use async, here's how I do it:

var client = new MongoClient("mongodb://" + server_username + ":" + server_password + "@" + server_host + ":" +  server_port);

List<MongoDB.Bson.BsonDocument> databases = client.ListDatabases();

Just one thing. It is in BsonDocument format that has 2 elements: "name" and "sizeOnDisk".

Hope this helps.

横笛休吹塞上声 2024-11-16 15:04:19

我无法使用现有答案验证给定的数据库是否存在,因此这是我的看法:

    // extension method on IMongoClient
    public static IMongoClient AssertDbExists(this IMongoClient client, string dbName)
    {
        bool dbFound = false;

        using(var cursor = client.ListDatabases())
        {
            var databaseDocuments = cursor.ToList();
            foreach (var db in databaseDocuments)
            {
                if (db["name"].ToString().Equals(dbName))
                {
                    dbFound = true;
                    break;
                }
            }
        }

        if (!dbFound) throw new ArgumentException("Can't connect to a specific database with the information provided", nameof(MongoSettings.ConnectionString));

        return client;
    }

然后像这样使用它:

// either you get the client with the DB validated or throws
_client = new MongoClient(settings.ConnectionString).AssertDbExists(_dbName);

使用: Mongo 官方 C# 驱动程序 v2.4.4

I wasn't able validate if a given DB exists or not with the existing answers, so here's my take on it:

    // extension method on IMongoClient
    public static IMongoClient AssertDbExists(this IMongoClient client, string dbName)
    {
        bool dbFound = false;

        using(var cursor = client.ListDatabases())
        {
            var databaseDocuments = cursor.ToList();
            foreach (var db in databaseDocuments)
            {
                if (db["name"].ToString().Equals(dbName))
                {
                    dbFound = true;
                    break;
                }
            }
        }

        if (!dbFound) throw new ArgumentException("Can't connect to a specific database with the information provided", nameof(MongoSettings.ConnectionString));

        return client;
    }

And then use it like this:

// either you get the client with the DB validated or throws
_client = new MongoClient(settings.ConnectionString).AssertDbExists(_dbName);

Using: Mongo Official C# driver v2.4.4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文