gpt4 book ai didi

node.js - 如果输入数组为空或 mongoose 聚合中不存在,如何跳过查询执行

转载 作者:太空宇宙 更新时间:2023-11-03 23:54:14 24 4
gpt4 key购买 nike

我需要使用聚合查询查找记录,如果给定的任何输入是空数组,则需要跳过该查询。在下面的代码中,我为“brandFilter”提供了一个空数组,然后该查询需要跳过 $in 执行。如果它有一些数组[一,二],那么它将给出相关的匹配输出

示例

{ $filter: {
input: '$products',
as: 'item',
cond: { $and: [
{
$gte: ['$$item.prodprice', Number(price)],
},
{ $in: [ "$$item.brand", brandArr ] }
]}
}}

最佳答案

以下过滤器将为我们提供预期的输出:

{
$filter:{
"input":"$products",
"as":"item",
"cond":{
$and:[
{
$gte: ['$$item.prodprice', Number(price)]
},
{
$or:[
{
$eq:[brandArr[0],undefined]
},
{
$in: [ "$$item.brand", brandArr ]
}
]
}
]
}
}
}

关于node.js - 如果输入数组为空或 mongoose 聚合中不存在,如何跳过查询执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066141/

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