$nor

On this page本页内容

Definition定义

$nor

$nor performs a logical NOR operation on an array of one or more query expression and selects the documents that failall the query expressions in the array. 对一个或多个查询表达式的数组执行逻辑NOR操作,并选择数组中所有查询表达式失败的文档。The $nor has the following syntax:$nor具有以下语法:

{ $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }

Examples示例

$nor Query with Two Expressions使用两个表达式的$nor查询

Consider the following query which uses only the $nor operator:考虑以下仅使用$nor运算符的查询:

db.inventory.find( { $nor: [ { price: 1.99 }, { sale: true } ]  } )

This query will return all documents that:此查询将返回以下所有文档:

  • contain the price field whose value is not equal to 1.99 and contain the sale field whose value is not equal to true or包含值不等于1.99price字段和值不等于truesale字段,或
  • contain the price field whose value is not equal to 1.99but do not contain the sale field or包含其值不等于1.99price字段,且不包含sale字段或
  • do not contain the price field but contain the sale field whose value is not equal to true or不包含price字段,且包含其值不等于truesale字段或
  • do not contain the price field and do not contain the sale field不包含price字段,也不包含sale字段

$nor and Additional Comparisons和其他比较

Consider the following query:考虑以下查询:

db.inventory.find( { $nor: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )

This query will select all documents in the inventory collection where:此查询将选择inventory集合中的所有文档,其中:

  • the price field value does not equal 1.99 andprice字段值不等于1.99并且
  • the qty field value is not less than 20 andqty字段值不小于20并且
  • the sale field value is not equal to truesale字段值不等于true

including those documents that do not contain these field(s).包括不包含这些字段的文档。

The exception in returning documents that do not contain the field in the $nor expression is when the $nor operator is used with the $exists operator.返回不包含$nor表达式中字段的文档时,例外情况是$nor运算符与$exists运算符一起使用。

$nor and $exists

Compare that with the following query which uses the $nor operator with the $exists operator:将其与以下使用$nor运算符和$exists运算符的查询进行比较:

db.inventory.find( { $nor: [ { price: 1.99 }, { price: { $exists: false } },
                             { sale: true }, { sale: { $exists: false } } ] } )

This query will return all documents that:此查询将返回以下所有文档:

  • contain the price field whose value is not equal to 1.99 and contain the sale field whose value is not equal to true包含值不等于1.99price字段和值不等于truesale字段
Tip提示
See also: 参阅:
←  $not$or →