【问题标题】:mongodb obtaining collection names c#mongodb获取集合名称c#
【发布时间】:2015-11-16 21:47:35
【问题描述】:

我正在尝试获取所有数据库的列表以及使用 Mongo C# 驱动程序进行连接的相关集合列表。

foreach (string database in server.GetDatabaseNames())
 {
  var db = client.GetDatabase(database);

  var col = ((MongoDatabase)db).GetCollectionNames();

   //Do something
    }

我能够获取数据库列表,但不能获取集合名称。不会过去的

         ((MongoDatabase)db).GetCollectionNames();

我可能错过了什么?

【问题讨论】:

  • 听起来像是一个 db 用户问题。您是否更改了用户或数据库服务器?
  • 你遇到了什么错误?
  • @CindyMeister 所以我实际上将它作为 Windows 服务运行,以从 mongodb 读取数据并将其转储到文件中。因此,当我尝试记录名称集合列表时。它甚至不会在这行代码之后创建事件或记录任何内容。
  • @Mate mongodb 托管在我的本地主机上。所以我没有改变任何东西
  • 你为什么要选角?

标签: c# mongodb mongodb-.net-driver


【解决方案1】:

这就是我解决这个问题的方法,因为接受的答案对我不起作用。

MongoDb 版本 3.4.0。
C# 驱动程序版本 2.4.3.23。

public List<string> GetCollections()
{
    List<string> collections = new List<string>();

    foreach (BsonDocument collection in _database.ListCollectionsAsync().Result.ToListAsync<BsonDocument>().Result)
    {
        string name = collection["name"].AsString;
        collections.Add(name);
    }

    return collections;
}

【讨论】:

  • 最好使用 non-async_database.ListCollections() 而不是使用 Result。
【解决方案2】:

MongoDB 2.6 版

mongodb-csharp 驱动:2.1.1

试试:

//user: root  pwd:pwd dbName:admin
  try
  {
    var client = new MongoClient("mongodb://root:pwd@localhost/admin");
    var db = client.GetDatabase("admin");
    foreach (var item in db.ListCollectionsAsync().Result.ToListAsync<BsonDocument>().Result)
     {
                Console.WriteLine(item.ToString());
     }
  } catch (Exception ex)
  {

    throw ex;
  }

重要提示:用户“root”必须存在于数据库中

以管理员身份在 cmd 上

C:\yourMongoServer\bin>mongo.exe --port 27017
use admin
db.createUser(
  {
    user: "root",
    pwd: "pwd",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

【讨论】:

  • 这行得通:) 我只需要使用“db.ListCollectionsAsync().Result.ToListAsync().Result”这一行
  • 你可以写 ListCollections().ToList()
  • item.ToString() 不仅提供集合名称,还提供元数据。 @VansFannel 的答案是最新的并且可以正常工作。
  • 为什么捕获异常只是为了再次抛出它?
【解决方案3】:

MongoDB 版本:3.4.6, C#驱动版本:2.4.4

工作解决方案:

        using (IAsyncCursor<BsonDocument> dbCursor = client.ListDatabases())
        {
            while (dbCursor.MoveNext())
            {
                foreach (var dbDoc in dbCursor.Current)
                {
                    Console.WriteLine("-----{0}-----", dbDoc["name"]);
                    var dbName = dbDoc["name"].ToString();  // list database name
                    using (IAsyncCursor<BsonDocument> collectionCursor = client.GetDatabase(dbName).ListCollections())
                    {
                        while (collectionCursor.MoveNext())
                        {
                            foreach (var collDoc in collectionCursor.Current)
                            {
                                Console.WriteLine(collDoc["name"]);  // list collection name
                            }
                        }
                    }
                }
            }
        }

【讨论】:

    【解决方案4】:

    在 MondoDB C# 驱动程序中,您有异步方法和返回异步游标的“同步”方法。 这允许这种紧凑的代码:

    List<string> collections = await (new MongoDB.Driver.MongoClient())
                               .GetDatabase("test")
                               .ListCollectionNames()
                               .ToListAsync();
    

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 2020-08-26
      • 2012-06-14
      相关资源
      最近更新 更多