$in
On this page
$in
The
$in
operator selects the documents where the value of a field equals any value in the specified array.
Compatibility
You can use $in
for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
To specify an $in
expression, use the following prototype:
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
For comparison of different BSON type values, see the specified BSON comparison order.
If the field
holds an array, then the $in
operator
selects the documents whose field
holds an array that contains
at least one element that matches a value in the specified array
(for example, <value1>
, <value2>
, and so on).
The $in
operator compares each parameter to each document
in the collection, which can lead to performance issues.
To improve performance:
- It is recommended that you limit the number of parameters passed to the
$in
operator to tens of values. Using hundreds of parameters or more can negatively impact query performance.
Create an index on the
field
you want to query.
Note
This document describes the $in
query operator.
For the $in
aggregation operator, see
$in (aggregation).
Query Data on Atlas by Using Atlas Search
For data stored in MongoDB Atlas, you can use the
Atlas Search in
operator when running $search
queries. Running
$in
after $search
is less performant
than running $search
with the in
operator.
To learn more about the Atlas Search version of this operator, see the in operator in the Atlas documentation.
Examples
Use the $in
Operator to Match Values
Consider the following example:
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
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:
{ _id: 1, item: "abc", qty: 10, tags: [ "school", "clothing" ], sale: false }
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"
.
db.inventory.update( { tags: { $in: ["appliances", "school"] } }, { $set: { sale:true } } )
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:
db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )
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
.