Getting exception while running the mongo query

While running the query

[[{"$sort":{“distance”:1,“price”:-1}},{"$unset":[“src”,“cms”,“dealer.email”,“dealer.locations”,“dealer.unknown”,“associatedDealers”]},{"$project":{"_id":1,“listing”:1,“bike_id”:1,“certified”:1,“color”:1,“condition”:1,“created_at”:1,“dealer”:1,“description”:1,“distance”:1,“estimatedPayment”:1,“mileage”:1,“model”:1,“name”:1,“photoUrls”:1,“price”:1,“ridingStyle”:1,“status”:1,“updated_at”:1,“vin”:1,“year”:1}},{"$group":{"_id":0,“count”:{"$sum":1},“document”:{"$push":"$$ROOT"}}},{"$project":{“count”:1,“document”:{"$slice":["$document",0,24]}}},{"$unwind":{“path”:"$document",“preserveNullAndEmptyArrays”:true}},{"$group":{"_id":0,“totalCount”:{"$first":"$count"},“data”:{"$push":"$document"}}},{"$limit":10}]]

getting following exception:

push used too much memory and cannot spill to disk. Memory limit: 104857600 bytes - {“operationTime”:“7088681554536300545”,“ok”:0,“code”:146,“codeName”:“ExceededMemoryLimit”,"$clusterTime":{“clusterTime”:“7088681554536300545”,“signature”:{“hash”:“xRoNutf0Qj3coUPQWZjGUmyDzW8=”,“keyId”:“7035637553084497921”}},“name”:“MongoError”}

You are taking your entire collection, every document, and pushing it into a single field (array) in $group. That exceeds the allowed document size. After that you are using $slice to only keep the first 24 elements of the array. There are a few ways to do this rather than pushing every document into an array - starting in version 5.2.0 in Atlas we support $firstN which allows you to specify it directly.

If you are on 5.0 or earlier there are some workarounds that have been described in this ticket: https://jira.mongodb.org/browse/SERVER-9377

Asya

2 Likes