$nin¶
-
$nin
¶ Syntax:
{ field: { $nin: [ <value1>, <value2> ... <valueN> ]} }
$nin
selects the documents where:- the
field
value is not in the specifiedarray
or - the
field
does 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
inventory
collection where theqty
field value does not equal5
nor15
. The selected documents will include those documents that do not contain theqty
field.If the
field
holds an array, then the$nin
operator selects the documents whosefield
holds 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 thesale
field value in theinventory
collection where thetags
field holds an array with no elements matching an element in the array["appliances", "school"]
or where a document does not contain thetags
field.The inequality operator
$nin
is not very selective since it often matches a large portion of the index. As a result, in many cases, a$nin
query with an index may perform no better than a$nin
query that must scan all documents in a collection. See also Query Selectivity.- the