我想获取一些文档,其中字段可能是$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/