On this page本页内容
$not
¶Syntax: { field: { $not: { <operator-expression> } } }
$not
performs a logical 对指定的NOT
operation on the specified <operator-expression>
and selects the documents that do not match the <operator-expression>
. <operator-expression>
执行逻辑NOT操作;并选择与<operator-expression>
不匹配的文档。This includes documents that do not contain the 这包括不包含该field
.field
的文档。
Consider the following query:考虑下面的查询:
This query will select all documents in the 此查询将选择inventory
collection where:inventory
集合中的所有文档,其中:
price
field value is less than or equal to 1.99
orprice
字段值小于或等于1.99
或price
field does not existprice
字段不存在{ $not: { $gt: 1.99 } }
is different from the $lte
operator. { $not: { $gt: 1.99 } }
与$lte
运算符不同。{ $lte: 1.99 }
returns only the documents where price
field exists and its value is less than or equal to 1.99
.{ $lte: 1.99 }
只返回存在价格字段且其值小于或等于1.99
的文档。
Remember that the 请记住,$not
operator only affects other operators and cannot check fields and documents independently. $not
运算符只影响其他运算符,不能单独检查字段和文档。So, use the 因此,使用$not
operator for logical disjunctions and the $ne
operator to test the contents of fields directly.$not
运算符进行逻辑析取,使用$ne
运算符直接测试字段的内容。
$not
The operation of the $not
operator is consistent with the behavior of other operators but may yield unexpected results with some data types like arrays.$not
运算符的操作与其他运算符的行为一致,但可能会对某些数据类型(如数组)产生意外的结果。
$not
$not
operator can perform logical 运算符可以对以下对象执行逻辑“非”操作:NOT
operation on:
/pattern/
)/pattern/
)
For example, the following query selects all documents in the 例如,以下查询选择inventory
collection where the item
field value does not start with the letter p
.inventory
集合中item
字段值不以字母p
开头的所有文档。
$regex
operator expression (Starting in MongoDB 4.0.7)$regex
运算符表达式(从MongoDB 4.0.7开始)
For example, the following query selects all documents in the 例如,以下查询选择inventory
collection where the item
field value does not start with the letter p
.inventory
集合中item
字段值不以字母p
开头的所有文档。
For example, the following PyMongo query uses Python’s 例如,以下PyMongo查询使用Python的re.compile()
method to compile a regular expression:re.compile()
方法编译正则表达式: