mongodb 如何在IMongoQueryable中添加聚合选项?

jdzmm42g  于 2023-02-18  发布在  Go
关注(0)|答案(2)|浏览(120)

我收到以下错误
命令聚合失败:排序超出了104857600字节的内存限制,但未选择加入外部排序。正在中止操作。请传递allowDiskUse:true以选择加入。
下面是我的示例代码,它有很多排序和大数据。我如何基于下面的示例允许DiskUse为true?

var collection = _context.GetCollection<SampleCollection>();

var result = collection.AsQueryable();

result = result.OrderByDescending(x => x.Date)
    .ThenByDescending(d => d.Status)
    .ThenByDescending(r => r.Firstname)
    .ThenByDescending(d => d.Lastname)
    .ThenByDescending(d => d.Birthdate)
    .ThenByDescending(d => d.Sex);

return result .ToList();
l0oc07j2

l0oc07j21#

我已经更新了我的代码以删除AsQueryable。

var patientList = _context.GetCollection<CommunityGetPatients>();

var aggregateOptions = new AggregateOptions { AllowDiskUse = true };

var result = patientList.Aggregate(aggregateOptions).Match(x => x.Status != Status.Deleted)
    .SortByDescending(x => x.Date)
        .ThenByDescending(d => d.Status)
        .ThenByDescending(r => r.Firstname)
        .ThenByDescending(d => d.Lastname)
        .ThenByDescending(d => d.Birthdate)

return result.ToList();
xurqigkl

xurqigkl2#

见下文:

var client = new MongoClient();
        var db = client.GetDatabase("db");
        var collection = db.GetCollection<SampleCollection>("c");
        var result = collection.AsQueryable<SampleCollection>(new AggregateOptions() { AllowDiskUse = true });

        var res = result.OrderByDescending(x => x.Date)
            .ThenByDescending(d => d.Status)
            .ToList();

相关问题