$in¶
On this page
-
$in¶ The
$inoperator selects the documents where the value of a field equals any value in the specified array. To specify an$inexpression, use the following prototype:For comparison of different BSON type values, see the specified BSON comparison order.
If the
fieldholds an array, then the$inoperator selects the documents whosefieldholds an array that contains at least one element that matches a value in the specified array (e.g.<value1>,<value2>, etc.)Changed in version 2.6: MongoDB 2.6 removes the combinatorial limit for the
$inoperator that exists for earlier versions of the operator.
Examples¶
Use the $in Operator to Match Values¶
Consider the following example:
This query selects all documents in the inventory
collection where the qty field value is either 5 or
15. 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.
Use the $in Operator to Match Values in an Array¶
The collection inventory contains documents that include the field
tags, as in the following:
Then, the following update() operation will
set the sale field value to true where the tags field holds
an array with at least one element matching either "appliances" or
"school".
For additional examples in querying arrays, see:
For additional examples in querying, see:
Use the $in Operator with a Regular Expression¶
The $in operator can specify matching values using regular
expressions of the form /pattern/. You cannot use $regex
operator expressions inside an $in.
Consider the following example:
This query selects all documents in the inventory collection where
the tags field holds either a string that starts with be or
st or an array with at least one element that starts with be or
st.
See also
find(), update(), $or, $set, $elemMatch.