Not Clause (Reference)

A not clause logically negates the truth value of a set of comparison clauses. When any of the comparison clauses is true, the not clause evaluates to false; when all of them are false, the not clause evaluates to true.

A not clause is an object member whose field is operator $not and whose value is an object whose members are comparison clauses, which are implicitly ANDed before negating the truth value of that conjunction.

"$not" : { comparison-clause ... }

Example: "$not" : {"$eq" : 200, "$lt" : 40}.

The following field-condition clause matches documents that have no field address.zip, as well as documents that have such a field but whose value is a scalar other than "90001" or an array that has no elements equal to "90001":

"address.zip" : {"$not" : {"$eq" : "90001"}}

In contrast, the following field-condition clause has the complementary effect: it matches documents that have a field address.zip whose value is either the scalar "90001" or an array that contains that scalar value.

"address.zip" : {"$eq" : "90001"}}

Here is an example of a field-condition clause with field salary and with value a not clause whose operand object has more than one comparison clause. It matches salary values that are not both greater than 20,000 and less than 100,000. That is, it matches salary values that are either less than or equal to 20,000 or greater than or equal to 100,000.

"salary" : {"$not" : {"$gt":20000, "$lt":100000}}

Related Topics