Database Manual / Reference / Query Language / Query Predicates / Logical

$not

Definition定义

$not
$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的文档。

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集合中的所有文档,其中:

  • the price field value is less than or equal to 1.99 orprice字段值小于或等于1.99,或
  • the 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 }仅返回存在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 inventory collection where the item field value does not start with the letter p.例如,以下查询选择inventory集合中item字段值以字母p开头的所有文档。

    db.inventory.find( { item: { $not: /^p.*/ } } )
  • $regex operator expression运算符表达式

    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开头的所有文档。

    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 re.compile() method to compile a regular expression:例如,以下PyMongo查询使用Python的re.compile()方法编译正则表达式:

    import re
    for noMatch in db.inventory.find( { "item": { "$not": re.compile("^p.*") } } ):
    print noMatch