MongoDB using NOT and AND together

You’re looking for NOT (A AND C), which is equivalent to NOT A OR NOT C:

db.collection.find({
  "$or": [
    {"institution_type": {"$ne": "A"}},
    {"type": {"$ne": "C"}}
  ]
})

MongoDB also has a $nor logical operator that “performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array”, so an equivalent query would be:

db.collection.find({
  "$nor": [
    {"institution_type": "A"},
    {"type": "C"}
  ]
})

The accepted answer recommends using a $where operator, but that is unnecessary here and taxes performance.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)