Cursor with Sort forEach exceeds maximum RAM, with Index

Hi,

I have a query that will return ~ 30k documents each of ~6k bytes ( if stringified ).

I am sorting the query, but have created an Index on that field, but the Cursor still returns the ''Sort operation used more than the maximum 33554432" error.

Here’s the Query…

     cartCursor.forEach(cart => {
            Carts.push(cart);
            if ( Carts.length == batch ) { sendCarts() }
                
      },e => {
           console.log('end',e)
           sendCarts();
       });

Any ideas?

Peter

Hello @Peter_Alderson,

The error ''Sort operation used more than the maximum 33554432" occurs when the sort operation requires more than 32 MB memory - the cursor.sort allows maximum 32 MB only (see MongoDB Limits and Thresholds - Sort Operations).

You can try using an Aggregation query instead, which can use more memory (100 MB) for the sort. In case the sort needs still more memory, you can use the allowDiskUse option.

Please post a sample document you are working with and the entire query showing the sort operation.