Definition定义
$nor$norperforms a logicalNORoperation on an array of one or more query predicates and selects the documents that fail all the query predicates in the array. The$norhas the following syntax:$nor对一个或多个查询谓词的数组执行逻辑NOR操作,并选择数组中所有查询谓词失败的文档。$nor具有以下语法:{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
Examples示例
$nor Query with Two Expressions使用两个表达式进行$nor查询
$nor Query with Two ExpressionsConsider 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包含值不等于pricefield whose value is not equal to1.99and contain thesalefield whose value is not equal totrueor1.99的price字段和值不等于true的sale字段,或contain the包含值不等于pricefield whose value is not equal to1.99but do not contain thesalefield or1.99的price字段,但不包含sale字段,或do not contain the不包含pricefield but contain thesalefield whose value is not equal totrueorprice字段,但包含值不等于true的sale字段,或do not contain the不包含pricefield and do not contain thesalefieldprice字段,也不包含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集合中的所有文档,其中:
thepricefield value does not equal1.99andprice字段值不等于1.99theqtyfield value is not less than20andqty字段值不小于20,并且thesalefield value is not equal totruesale字段值不等于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运算符与$exists运算符一起使用时,返回不包含$nor表达式中字段的文档时会出现异常。
$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包含值不等于pricefield whose value is not equal to1.99and contain thesalefield whose value is not equal totrue1.99的price字段和值不等于true的sale字段