gpt4 book ai didi

javascript - MongoDB $in 和 $ne

转载 作者:行者123 更新时间:2023-11-28 16:50:38 26 4
gpt4 key购买 nike

我想获取一些文档,其中字段可能是$in某个数组(如果设置了过滤器,则进行条件查询)。

但是,我还需要无条件检查同一字段是否为 $ne: null。我知道 $in: [some value] 可用于防止检索 null,但问题是查询的一部分是基于是否设置了过滤器的条件。

是否有更有效的方法来执行以下操作?这会起作用吗?

db.Parents.fetch({
childId: { $in: childIds },
childId: { $ne: null }
});

最佳答案

使用$and运算符

db.Parents.fetch({
$and:[
{childId: { $in: childIds }},
{childId: { $ne: null }}
]
});

关于javascript - MongoDB $in 和 $ne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60009342/

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