Exceeded memory limit for $group, but didn't allow external sort


Exceeded memory limit for $group, but didn’t allow external sort

因为Mongodb的聚合操作管道的输出结果不能超过16M,超过16M会出现错误。

  • Mongodb

allowDiskUse: true

1
2
3
4
5
6
7
8
9
db.stocks.aggregate(
[
{ $project : { cusip: 1, date: 1, price: 1, _id: 0 } },
{ $sort : { cusip : 1, date: 1 } }
],
{
allowDiskUse: true
}
);
  • Mongoose

allowDiskUse(true)

1
await stocks.aggregate([{ $match:{price:1}}, {$group: {_id:{date:"$date"}, sum:{$sum:"$price"}}}]).allowDiskUse(true);