gpt4 book ai didi

node.js - 为什么 mongoose 中的 $limit 聚合不返回所有结果?

转载 作者:太空宇宙 更新时间:2023-11-04 00:11:30 27 4
gpt4 key购买 nike

我已经执行了聚合查询,该查询具有$limit,如果我将1到任意之间的数字传递给它,它就可以正常工作 > 正整数。现在,我需要除通过 limit: 0 之外的所有结果,因此,这是我的错误。

Assert: command failed: { "ok" : 0, "errmsg" : "the limit must be positive", "code" : 15958, "codeName" : "Location15958" } : aggregate failed

查询

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

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

最佳答案

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

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

关于node.js - 为什么 mongoose 中的 $limit 聚合不返回所有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49299734/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com