$not¶
-
$not¶ Syntax:
{ field: { $not: { <operator-expression> } } }$notperforms a logicalNOToperation on the specified<operator-expression>and selects the documents that do not match the<operator-expression>. This includes documents that do not contain thefield.Consider the following query:
This query will select all documents in the
inventorycollection where:- the
pricefield value is less than or equal to1.99or - the
pricefield does not exist
{ $not: { $gt: 1.99 } }is different from the$lteoperator.{ $lte: 1.99 }returns only the documents wherepricefield exists and its value is less than or equal to1.99.Remember that the
$notoperator only affects other operators and cannot check fields and documents independently. So, use the$notoperator for logical disjunctions and the$neoperator to test the contents of fields directly.Consider the following behaviors when using the
$notoperator:The operation of the
$notoperator is consistent with the behavior of other operators but may yield unexpected results with some data types like arrays.The
$notoperator does not support operations with the$regexoperator. Instead use//or in your driver interfaces, use your language’s regular expression capability to create regular expression objects.Consider the following example which uses the pattern match expression
//:The query will select all documents in the
inventorycollection where theitemfield value does not start with the letterp.If you are using Python, you can write the above query with the PyMongo driver and Python’s
python:re.compile()method to compile a regular expression, as follows:
- the