代码之家  ›  专栏  ›  技术社区  ›  bjnsn

sort()操作超出了MongoDB Stitch函数中的最大内存,尽管有限制()

  •  0
  • bjnsn  · 技术社区  · 7 年前

    我正在试图解决最近在 Stitch 查询MongoDB数据库的函数。

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

    我收到此错误:

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

    据MongoDB称 docs :

    当一个$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,所以我可能错过了一些非常明显的东西。

    0 回复  |  直到 5 年前
    推荐文章