$nin¶
On this page
-
$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.
If the field
holds an array, then the $nin
operator selects
the documents whose field
holds an array with no element equal to
a value in the specified array (for example, <value1>
,
<value2>
, and so on).
For comparison of different BSON type values, see the specified BSON comparison order.
Examples¶
Create the inventory
collection:
Select on Unmatching Documents¶
The following query selects all documents from the inventory
collection where the quantity
does not equal either 5 or 15.
The query also matches documents that do not have a quantity
field.
Example output:
Select on Elements Not in an Array¶
Set the exclude
field to true
for documents that don’t have the
"school"
tag.
updateMany()
also selects a document when the
document does not contain the field $nin
is matching on.
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.
See also