Search code examples
c#.netmongodbmongodb-querymongodb-.net-driver

Mongodb C# Query Sort exceeded memory limit of


I have 2 weeks learning and working with MongoDB, I'm building a simple WinForm APP with a DataGridview.

Everything was working fine but I added more than 1.000.000 documents and now it show me this error:

MongoDB.Driver.MongoCommandException: 'Command aggregate failed: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in..'

I've checked all my weekend google how to use Aggreate allowDiskUse:true but it doesnt works either.

Thanks.

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();
    }
}

Solution

  • You should be able to pass in aggregator options to the queryable.

    collection.AsQueryable(new AggregateOptions { AllowDiskUse = true })
    

    However, usually, going over the document limit with a sort is a sign that your query isn't optimal. You may want to rethink your db design or query so that you don't have to sort such a large set of data.