On this page
$nor
在本页面
{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
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.99的price字段,以及值不等于true的sale字段