$nor

在本页面

  • $nor
    • $nor对一个或多个查询表达式的数组执行逻辑NOR运算,并选择使数组中的所有查询表达式“失败”的文档。 $nor具有以下语法:
{ $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }

See also

Examples

$ nor 带有两个表达式的查询

考虑以下仅使用$nor运算符的查询:

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

该查询将返回以下所有文档:

  • 包含price字段,其值等于1.99,并包含sale字段,其值等于true

  • 包含price字段,其值等于1.99 但是不*包含sale字段

  • 包含price字段包含sale字段,其值等于true

  • 包含price字段不*包含sale字段

$ nor 和其他比较

考虑以下查询:

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

此查询将选择inventory集合中的所有文档,其中:

  • price字段值等于1.99

  • qty字段的值*不小于20 并且

  • sale字段的值等于true

包括那些不包含这些字段的文档。

$nor运算符与$exists运算符一起使用时,返回不包含$nor表达式中的字段的文档的 exception。

$ nor 和$ exists

将其与使用$nor运算符和$exists运算符的以下查询进行比较:

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

该查询将返回以下所有文档:

  • 包含值不等于1.99price字段,以及值不等于truesale字段