$nor

  • $nor
  • $nor performs a logical NOR operation on an arrayof one or more query expression and selects the documents that failall the query expressions in the array. The $nor hasthe following syntax:
  1. { $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }

See also

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

Examples

$nor Query with Two Expressions

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

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

$nor and Additional Comparisons

Consider the following query:

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

This query will select all documents in the inventory collectionwhere:

  • 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 fieldin the $nor expression is when the $nor operator isused with the $exists operator.

$nor and $exists

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

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

This query will return all documents that:

  • contain the price field whose value is not equal to 1.99and contain the sale field whose value is not equal totrue