I\'m trying to negate an $and clause with MongoDB and I\'m getting a MongoError: invalid operator: $and message back. Basically what I want to achieve
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.