0

我正在尝试解决最近在查询 MongoDB 数据库的Stitch函数上出现的问题。

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;
};

我收到此错误:

查找命令期间的执行器错误:OperationFailed:排序操作使用的 RAM 超过了最大 33554432 字节。添加索引,或指定较小的限制。

根据 MongoDB文档

当 $sort 紧跟在管道中的 $limit 之前时,$sort 操作只在进行时维护前 n 个结果,其中 n 是指定的限制,MongoDB 只需要在内存中存储 n 个项目。当 allowDiskUse 为 true 并且 n 项超过聚合内存限制时,此优化仍然适用。

在 2.4 版更改:在 MongoDB 2.4 之前,$sort 会对内存中的所有结果进行排序,然后将结果限制为 n 个结果。

如果我理解正确,这意味着以下应该可以工作(因为集群使用的是 MongoDB 版本 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;
};

但是,我收到与以前相同的错误。

我不熟悉 Mongo/Stitch,所以我可能会遗漏一些非常明显的东西。

4

0 回答 0