Sort exceeded memory limit: using $group and $first in large result sets

Hi community,

I have followed this tutorial code. In order to have the first sales date, the sorting is absolutely necessary. But what if there are 500 million sales and the total number of sales exceeds the maximum sort size?

     { $sort: { item: 1, date: 1 } },
           _id: "$item",
           firstSalesDate: { $first: "$date" }

This is the error I’d get if the resultset is too large

MongoServerError: PlanExecutor error during aggregation :: caused by :: Sort exceeded memory limit of 33554432 bytes, but did not opt in to external sorting.

Any ideas how to solve this?

If you sort using the those keys often you must have an index.