Definition定义
$not$notperforms a logical对指定的NOToperation 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的文档。
Compatibility兼容性
You can use 您可以将$not for deployments hosted in the following environments:$not用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The $not operator has the following form:$not运算符具有以下形式:
{ field: { $not: { <operator-expression> } } }
Consider the following example:考虑以下示例:
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
This query will select all documents in the 此查询将选择inventory collection where:inventory集合中的所有文档,其中:
thepricefield value is less than or equal to1.99orprice字段值小于或等于1.99,或thepricefield 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 }仅返回存在price字段且其值小于或等于1.99的文档。
You must use the 您必须将$not operator with another operator expression. For example, to use $not to perform an inequality check, use this syntax:$not运算符与另一个运算符表达式一起使用。例如,要使用$not执行不等式检查,请使用以下语法:
{ price: { $not: { $eq: 1.99 } } }
The preceding query is equivalent to:前面的查询等价于:
{ price: { $ne: 1.99 } }
The following 以下$not query is invalid because it attempts to compare a field without an operator:$not查询无效,因为它试图比较没有运算符的字段:
{ price: { $not: 1.99 } }Behavior行为
Arrays数组
When passed an array argument, the 当传递数组参数时,$not operator may yield unexpected results. To match documents based on multiple false conditions, use $nor.$not运算符可能会产生意外结果。要根据多个错误条件匹配文档,请使用$nor。
Regular Expressions正则表达式
$not operator can perform logical 运算符可以对以下对象执行逻辑NOT operation on:NOT操作:
Regular expression objects (i.e.正则表达式对象(例如/pattern/)/pattern/)For example, the following query selects all documents in the例如,以下查询选择inventorycollection where theitemfield value does not start with the letterp.inventory集合中item字段值不以字母p开头的所有文档。db.inventory.find( { item: { $not: /^p.*/ } } )$regexoperator expression运算符表达式For example, the following query selects all documents in the例如,以下查询选择inventorycollection where theitemfield value does not start with the letterp.inventory集合中item字段值不以字母p开头的所有文档。db.inventory.find( { item: { $not: { $regex: "^p.*" } } } )
db.inventory.find( { item: { $not: { $regex: /^p.*/ } } } )driver language's regular expression objects驱动程序语言的正则表达式对象For example, the following PyMongo query uses Python's例如,以下PyMongo查询使用Python的re.compile()method to compile a regular expression:re.compile()方法编译正则表达式:import re
for noMatch in db.inventory.find( { "item": { "$not": re.compile("^p.*") } } ):
print noMatch