Mongodb C# 查询排序超出了内存限制

fab*_*vic 3 .net c# mongodb mongodb-query mongodb-.net-driver

我有 2 周的时间学习和使用 MongoDB,我正在使用 DataGridview 构建一个简单的 WinForm 应用程序。

一切正常,但我添加了超过 1.000.000 个文档,现在它显示了这个错误:

MongoDB.Driver.MongoCommandException: '命令聚合失败:排序超出了 104857600 字节的内存限制,但没有选择外部排序。中止操作。通过 allowDiskUse:true 选择加入..

我整个周末都在谷歌上检查了如何使用 Aggreate allowDiskUse:true 但它也不起作用。

谢谢。

public void ReadAllDocuments()
{
    List<Clientes> list = collection.AsQueryable().OrderBy(q => q.Nombre).ToList();

    dataGridView1.DataSource = list;
    if (dataGridView1.Rows.Count > 0)
    {
        textBox1.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
        textBox2.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();
        textBox3.Text = dataGridView1.Rows[0].Cells[2].Value.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

您应该能够将聚合器选项传递给可查询的。

collection.AsQueryable(new AggregateOptions { AllowDiskUse = true })
Run Code Online (Sandbox Code Playgroud)

但是,通常,通过排序超过文档限制表明您的查询不是最佳的。您可能需要重新考虑您的数据库设计或查询,以便您不必对如此庞大的数据集进行排序。