user2402616
user2402616

Reputation: 1563

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).

However, I need to also unconditionally check whether this same field is $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.

Is there a more efficient way of doing what's below? Will that even work?

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

Upvotes: 0

Views: 1146

Answers (1)

Valijon
Valijon

Reputation: 13113

Use $and operator

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

Upvotes: 3

Related Questions