Mongo Error QueryExceededMemoryLimitNoDiskUseAllowed

Viewed 5072

Getting this error, when I am using this query:

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"}}])

What change do I need to do? I do not want to change the query, so how to increase the limit of this, or I need to go some data archived method?

3 Answers

Add allowDiskUse and set it to true at the end of your query so it becomes like the following:

 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)

Add allowDiskUse with second params of aggregate function, like this:

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)

you can add allowDiskUse with second of aggregate function eg:

db.getCollection("loans_unique").aggregate([{
    "$group": {
        "_id": {
            phone: "$phone",
            address: "$address",
            title: "$title"
        },
        "count": {
            "$sum": 1
        }
       }
},
    {
        $sort: {
            "count": -1
        }
    },
    {
        $match: {
            "count": {$gt: 1}
        }
    }
], {
    allowDiskUse: true
})
Related