Mongo 错误 QueryExceededMemoryLimitNoDiskUseAllowed

Mongo Error QueryExceededMemoryLimitNoDiskUseAllowed

出现这个错误,当我使用这个查询时:

db2.collection('candata')
.aggregate([{$sort:{time:-1}},{$group:{_id:{batteryId:"$batteryId"},soctime:{$first:"$time"},GPSStatus:{$first:"$GPSStatus"},CANStatus:{$first:"$CANStatus"},soc: { $first : "$socpercentage" },charging_status: { $first : "$charging_status" },status: { $first : "$status" }}},{$sort:{'_id.batteryId':1}},{$lookup:{from : "swap_table",localField : "_id.batteryId",foreignField:"batteryId",as:"swap"}},{$lookup:{from : "battery_status_table",localField : "_id.batteryId",foreignField:"batteryId",as:"battery_statustb"}}])

我需要做什么改变?我不想改变查询,那么如何增加这个限制,或者我需要去一些数据归档的方法?

添加 allowDiskUse 并在查询结束时将其设置为 true,这样它就变成如下所示:

 db2.collection('candata')
.aggregate([{$sort:{time:-1}},{$group:{_id:{batteryId:"$batteryId"},soctime:{$first:"$time"},GPSStatus:{$first:"$GPSStatus"},CANStatus:{$first:"$CANStatus"},soc: { $first : "$socpercentage" },charging_status: { $first : "$charging_status" },status: { $first : "$status" }}},{$sort:{'_id.batteryId':1}},{$lookup:{from : "swap_table",localField : "_id.batteryId",foreignField:"batteryId",as:"swap"}},{$lookup:{from : "battery_status_table",localField : "_id.batteryId",foreignField:"batteryId",as:"battery_statustb"}}]).allowDiskUse(true)

在聚合函数的第二个参数中添加 allowDiskUse,如下所示:

agg = [{$sort:{time:-1}},{$group:{_id:{batteryId:"$batteryId"},soctime:{$first:"$time"},GPSStatus:{$first:"$GPSStatus"},CANStatus:{$first:"$CANStatus"},soc: { $first : "$socpercentage" },charging_status: { $first : "$charging_status" },status: { $first : "$status" }}},{$sort:{'_id.batteryId':1}},{$lookup:{from : "swap_table",localField : "_id.batteryId",foreignField:"batteryId",as:"swap"}},{$lookup:{from : "battery_status_table",localField : "_id.batteryId",foreignField:"batteryId",as:"battery_statustb"}}]
db2.collection('candata').aggregate(agg, allowDiskUse=True)