I'm trying to fix an issue that recently cropped up on a Stitch function querying a MongoDB database.
exports = function readMostRecentDataFromCollection(c) {
var mongodb = context.services.get('mongodb-atlas');
var coll = mongodb.db('lexicon').collection(c);
var result = coll
.find({
$and: [{ data: { $exists: true } }, { time: { $exists: true } }]
})
.sort({ time: -1 })
.toArray()[0];
return result;
};
I receive this error:
Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.
According to the MongoDB docs:
When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory. This optimization still applies when allowDiskUse is true and the n items exceed the aggregation memory limit.
Changed in version 2.4: Before MongoDB 2.4, $sort would sort all the results in memory, and then limit the results to n results.
If I understand correctly, that means the following should work (since the cluster is using MongoDB version 3.4.14):
exports = function readMostRecentDataFromCollection(c) {
var mongodb = context.services.get('mongodb-atlas');
var coll = mongodb.db('lexicon').collection(c);
var result = coll
.find({
$and: [{ data: { $exists: true } }, { time: { $exists: true } }]
})
.sort({ time: -1 })
.limit(1) // adding limit
.toArray()[0];
return result;
};
However, I receive the same error as before.
I'm not familiar with Mongo/Stitch so I could be missing something quite obvious.