$nin¶
-
$nin¶ Syntax:
{ field: { $nin: [ <value1>, <value2> ... <valueN> ]} }$ninselects the documents where:- the
fieldvalue is not in the specifiedarrayor - the
fielddoes not exist.
For comparison of different BSON type values, see the specified BSON comparison order.
Consider the following query:
This query will select all documents in the
inventorycollection where theqtyfield value does not equal5nor15. The selected documents will include those documents that do not contain theqtyfield.If the
fieldholds an array, then the$ninoperator selects the documents whosefieldholds an array with no element equal to a value in the specified array (e.g.<value1>,<value2>, etc.).Consider the following query:
This
update()operation will set thesalefield value in theinventorycollection where thetagsfield holds an array with no elements matching an element in the array["appliances", "school"]or where a document does not contain thetagsfield.The inequality operator
$ninis not very selective since it often matches a large portion of the index. As a result, in many cases, a$ninquery with an index may perform no better than a$ninquery that must scan all documents in a collection. See also Query Selectivity.- the