发布于 2015-09-14 14:59:18 | 226 次阅读 | 评论: 0 | 来源: 网络整理

$nor

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

$nor performs a logical NOR operation on an array of two or more <expressions> and selects the documents that fail all the <expressions> in the array.

Consider the following example:

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

This query will select all documents in the inventory collection where:

  • the price field value does not equal 1.99 and
  • the qty field value is not less than 20 and
  • the sale field value is not equal to 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.

Consider the following query which uses only the $nor operator:

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
  • contain the price field whose value is not equal to 1.99 but do not contain the sale field or
  • do not contain the price field but contain the sale field whose value is not equal to true or
  • do not contain the price field and do not contain the sale field

Compare that with the following query which uses the $nor operator with the $exists operator:

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

也可以参考

find(), update(), $set, $exists.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务