Attempting to retrieve entries from my mongoDB database one by one, I encountered an issue after successfully loading around 400 out of 1000 entries. The error message reported was:
Executor error during find command :: caused by :: Sort exceeded memory limit of 33554432 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.
The Axios get request structure is as follows:
for (let i = 0; i < total; i++) {
await axios({
method: "GET",
url: "/api/entries/",
params: { from: index + i, _limit: 1 },
})
.then((res) => {
setPics((prev) => {
return [...new Set([...prev, ...res.data])];
});
setIndex((prev) => prev++)
setMoreEntries(res.data.length > 0)
})
}
In the controller, the GET function implementation is as shown below:
const getEntries = asyncHandler(async (req, res) => {
const entries = await Entry.find().allowDiskUse(true).sort({ createdAt: 'desc' }).skip(req.query.from).limit(req.query._limit)
res.status(200).json(entries)
})
Everything functions flawlessly until about halfway through the loading process when it encounters a breaking point. Although I attempted using allowDiskUse(true)
to address the issue, the same outcome persisted.
Note: Removing the .sort({ createdAt: 'desc' })
resolves the problem, however, the order of entries loaded becomes inverted.