【问题标题】:Why $limit in mongoose with aggregation not return all results?为什么 $limit 在 mongoose 中聚合不返回所有结果?
【发布时间】:2018-03-15 20:57:02
【问题描述】:

我关注aggregation 查询,它有$limit 我已经设置如果我在1 to any 正整数之间传递一个数字,它就可以正常工作。现在,我需要所有结果而不是通过 limit: 0 所以,这是通过我的错误。

断言:命令失败:{ “好”:0, "errmsg" : "限制必须是正数", “代码”:15958, “代号”:“位置 15958” } : 聚合失败

查询

var limit = 0; // working with 1 to infinite ( any positive integrer ) 
db.getCollection('abc').aggregate([
    {
        $match : {}
    },
    {
        $limit: limit
    }
])

如果有人知道我应该如何使用$limit从数据库中返回所有文档,请让我摆脱困境。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    构建您的管道以仅在需要时包含 $limit 元素:

    var pipeline = [{$match: {}}];
    if (limit) {
        pipeline.push({$limit: limit});
    }
    db.getCollection('abc').aggregate(pipeline);
    

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 2019-01-14
      • 2023-03-17
      • 2020-09-11
      • 2021-12-18
      • 2018-06-20
      • 2021-12-06
      • 1970-01-01
      • 2021-06-11
      相关资源
      最近更新 更多