我正在使用一个包含数百万条记录的集合进行异步填充,因此无法保证顺序。
在查询MongoDB FindAsync方法时,可以添加过滤器,但不能添加排序。
如何确保使用IAsyncCursor返回的记录顺序?我在我的集合中有一个按时间戳字段升序索引,这足以保证排序吗?
====================
使用FindAsync方法返回IAsyncCursor。 记录以任意批次返回,可以使用以下 using/while 结构进行处理。
var collection = _database.GetCollection<BsonDocument>("restaurants");
var filter = new BsonDocument();
var count = 0;
using (var cursor = await collection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
// What order will these records be in? how do I guarantee order ascending by epochtimestamp?;
}
}
}
每个异步“批次”会根据整个查询进行排序吗?我如何确保记录按正确顺序排列?
epocdatestamp字段上的升序索引将允许我返回完整排序的列表(由于记录数量很多,需要很长时间)。
我是否应该一次性将完整排序列表按所需顺序重新写回数据库,以便将来的IAsyncCursor查询将按顺序返回记录?
我需要这样做吗?还是升序索引足够了?