【问题标题】:Command getMore failed: End of file MongoDB命令 getMore 失败:文件结束 MongoDB
【发布时间】:2019-02-08 09:33:22
【问题描述】:
    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。 但现在我面临foreachmongoCursor 循环中的Command getMore failed: End of file。

【问题讨论】:

标签: c# mongodb


【解决方案1】:

使用异步游标 /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();
      }
    }
  }

只要测试它所提供的东西。

【讨论】:

  • 感谢您的回复。我已经使用了上面的异步代码,但仍然抛出以下错误:[Inner Exception] Message: Command getMore failed: End of file。堆栈跟踪:在 MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.ProcessReply(ConnectionId connectionId, ReplyMessage1 回复)在 MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.d__11.MoveNext()
  • 只测试而不写你的“一些查询”。我认为它的查询语法问题或查询和条件错误或耗时。调试内部查询并尝试优化它 .mongoCollection.Find(Query.And(some query))
猜你喜欢
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多