$in (original) (raw)
$in
The $in operator selects the documents where the value of a field equals any value in the specified array.
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
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).
For data stored in MongoDB Atlas, you can use theAtlas Search inoperator when running $search queries. Running$in after $search is less performant than running $search with the inoperator.
To learn more about the Atlas Search version of this operator, see the in operator in the Atlas documentation.
Create the inventory
collection:
db.inventory.insertMany( [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] )
Consider the following example:
db.inventory.find( { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } )
This query selects all documents in the inventory
collection where the value of the quantity
field is either 5 or 15.
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.
The following updateMany() operation sets theexclude
field to false
when the tags
array has at least one element that matches either "home"
or "school"
.
db.inventory.updateMany(
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
)
Example output:
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
For additional examples on querying arrays, see:
For additional examples on querying, seeQuery Documents.
The $in operator can specify matching values using regular expressions of the form /pattern/
. You cannot use $regexoperator 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
orst
or an array with at least one element that starts with be
orst
.
See also: