c# - 命令 getMore 失败 : End of file MongoDB

标签 c# mongodb

    MongoCursor<BsonDocument> mongoCursor = 
      mongoCollection.Find(Query.And(some query))
       .SetFlags(QueryFlags.NoCursorTimeout)
       .SetFields(idFieldName);

    int totalCount = 0;
    Queue<List<long>> idBatchQueue = new Queue<List<long>>();
    List<long> idBatch = new List<long>(batchSize);
    foreach (BsonDocument document in mongoCursor)
    {
        idBatch.Add(document[idFieldName].ToInt64());
        if (idBatch.Count >= batchSize)
        {
            idBatchQueue.Enqueue(idBatch);
            totalCount += idBatch.Count;
            idBatch = new List<long>(batchSize);
        }
    }

首先,我遇到了 Command getMore failed: Cursor not found, cursor id:xxx 错误,因此我添加了标志 QueryFlags.NoCursorTimeout。 但是现在我在 mongoCursorforeach 循环中面临 Command getMore failed: End of file

最佳答案

使用异步游标/FindAsync 就可以了

var client = new MongoClient();

  IMongoDatabase db = client.GetDatabase("school");

  var collection = db.GetCollection<BsonDocument>("students");

  using (IAsyncCursor<BsonDocument> cursor = await collection.FindAsync(new BsonDocument()))
  {
    while (await cursor.MoveNextAsync())
    {
      IEnumerable<BsonDocument> batch = cursor.Current;
      foreach (BsonDocument document in batch)
      {
        Console.WriteLine(document);
        Console.WriteLine();
      }
    }
  }

就像它给出的那样测试。

关于c# - 命令 getMore 失败 : End of file MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52147842/

相关文章:

mongodb $addtoset 保证添加到数组末尾吗?

django - 使用 MongoEngine 创建用户注册页面

c# - TFS 内部版本 : the auto merge oprtion is not supported for the conflict on item

C# 随机生成

c# - 淡入淡出启动画面

MongoDB 搜索子数组中不存在的值

node.js - Mongodb试图让选定的字段从聚合中返回

java - 用 Java 访问 MongoDB 中的数组元素

c# - 比赛结果平局,如何在基座上列出两个名字?

c# - 更改 ViewModel 的 View