Get the App
SLTechnology News&Howtos  ›  Database  › 

Solve the problem that MongoDB sorting exceeds the memory limit

Shulou Source: shulou.com Published: 2022-06-01 04:35:53 10月02日 Update

Perform a large sort operation (such as aggregation) on the collection with the following error: (test version: MongoDB 3.0.6)

> db.bigdata.aggregate ({$group: {_ id: "$range", total: {$sum: 1}}, {$sort: {total:-1}}); #. Aggregate failed at Error () at doassert (src/mongo/shell/assert.js:11:14) #... Error: command failed: {"errmsg": "exception: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.", "code": 16819, "ok": 0}

Solution method

Reference document: Memory Restrictions

In MongoDB, the maximum internal sort large memory limit is 100m, and if you perform a larger sort, you need to use the allowDiskUse option to write the data to a temporary file to sort.

Add the allowDiskUse option to the query statement:

Db.bigdata.aggregate ([{$group: {_ id: "$range", total: {$sum: 1}, {$sort: {total:-1}}], {allowDiskUse: true})

Summary

The above is introduced by the editor to solve the problem of MongoDB sorting exceeding the memory limit. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!

Tags: Sort limit memory problem maximum data file document method version question website statement error internal reference help support query test Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Docker vpn MySQL