$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:{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }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 (for example,<value1>,<value2>, and so on).The
$inoperator 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
$inoperator to tens of values. Using hundreds of parameters or more can negatively impact query performance. -
Create an index on the
fieldyou want to query.
Note
This document describes the
$inquery operator. For the$inaggregation operator, see $in (aggregation). -
Examples
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" ] } ] )
Use the $in Operator to Match Values
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.
Use the $in Operator to Match Values in an Array
The following updateMany() operation sets the exclude 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: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
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.