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);
}
}
首先,我遇到命令getMore失败:找不到光标,光标ID:xxx 错误,为此我添加了标志QueryFlags.NoCursorTimeout
。
但是现在我面对的是{strong>命令getMore失败:foreach
的{{1}}循环中的文件结尾。
答案 0 :(得分:0)
使用异步光标/ 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();
}
}
}
只需对其提供的功能进行测试即可。