So I'm running a Node.js server with mongoose and I've been trying to download a large file using the GridFS node package but I've been getting the following error:
MongoServerError: Executor error during find command :: caused by :: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting.
Here is the code that I've been using to download the file:
// Stream a single file.
router.get('/stream/:filename', async function(req, res, next){
try{
const file = gfs.find({
filename: req.params.filename
}).toArray((err, files) => {
if (!files || files.length === 0) {
return res.status(404)
.json({
err: "no files exist"
});
}
gfs.openDownloadStreamByName(req.params.filename)
.pipe(res);
});
}catch(err){
return res.status(400).json({
err:'Could not fetch the file.'
})
}
});
Given that it's OK to answer your own question on StackOverflow, I’d like to document this problem so that my solution may help others encountering it later.