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
。
但是现在我在 mongoCursor
的 foreach
循环中面临 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/