3
    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);
        }
    }

Firstly i was facing Command getMore failed: Cursor not found, cursor id:xxx error so for that i have added flag QueryFlags.NoCursorTimeout. But now i am facing Command getMore failed: End of file in foreach loop of mongoCursor.

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
Mahesh
  • 567
  • 2
  • 14
  • https://stackoverflow.com/questions/44248108/mongodb-error-getmore-command-failed-cursor-not-found – Dipen Shah Sep 05 '18 at 18:21
  • Thanks for your suggestion but i have already wen through this document and its not for resolving end of file issue. – Mahesh Sep 06 '18 at 11:43

1 Answers1

0

use async cursor /FindAsync and it will work

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();
      }
    }
  }

Just test like it what it gives.

Jin Thakur
  • 2,711
  • 18
  • 15
  • Thanks for your reply. I have used above async code but still its throwing following error: [Inner Exception] Message: Command getMore failed: End of file. Stack Trace: at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply) at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.d__11.MoveNext() – Mahesh Sep 17 '18 at 10:23
  • Just test without writing your "some query". I think its query syntax issue or query and condition is wrong or time consuming. debug the inner query and try to refine it .mongoCollection.Find(Query.And(some query)) – Jin Thakur Sep 17 '18 at 16:26