简体   繁体   English

MongoDB $in 和 $ne

[英]MongoDB $in and $ne

I'd like to fetch some Documents where a field can possibly be $in some array (conditional query if a filter was set).我想获取一些文档,其中某个字段可能$in某个数组中(如果设置了过滤器,则进行条件查询)。

However, I need to also unconditionally check whether this same field is $ne: null .但是,我还需要无条件地检查该字段是否为$ne: null I understand the $in: [some values] can be used to prevent null from being retrieved, but the problem is that part of the query is conditional based on if a filter is set.我了解$in: [some values]可用于防止检索 null,但问题是查询的一部分是基于是否设置过滤器的条件。

Is there a more efficient way of doing what's below?有没有更有效的方法来做下面的事情? Will that even work?这甚至会起作用吗?

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

Use $and operator使用$and运算符

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM