$in¶
-
$in
¶ Syntax:
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
$in
selects the documents where thefield
value equals any value in the specified array (e.g.<value1>
,<value2>
, etc.)Consider the following example:
This query selects all documents in the
inventory
collection where theqty
field value is either5
or15
. Although you can express this query using the$or
operator, choose the$in
operator rather than the$or
operator when performing equality checks on the same field.If the
field
holds an array, then the$in
operator selects the documents whosefield
holds an array that contains at least one element that matches a value in the specified array (e.g.<value1>
,<value2>
, etc.)Consider the following example:
This
update()
operation will set thesale
field value in theinventory
collection where thetags
field holds an array with at least one element matching an element in the array["appliances", "school"]
.