Wednesday, September 12, 2018

Command getMore failed: End of file MongoDB

Leave a Comment
    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.

1 Answers

Answers 1

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment