0

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.

Community
  • 1
  • 1
bjnsn
  • 2,710
  • 2
  • 16
  • 14
  • maybe it's something with stitch. Have you tried running the query using command line to see if the error persists? – Graciano Mar 28 '18 at 19:30
  • This seems to be a limitation when interacting within a Stitch function. I solved my issue by adding an index via the mongo shell instead (`db.mycollection.createIndex({'time': -1}`). – bjnsn Mar 28 '18 at 21:49
  • @bjnsn you also can use MongoDB Compass for creating indexes. i am building an application using MongoDB Stitch and using it. – Muhammad Furqan Mar 29 '18 at 11:47

0 Answers0