> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $in (query predicate operator)

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](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The `$in` operator has the following form:

```javascript
{ field: { $in: [ <value1>, <value2>, ... <valueN> ] } }
```

**Note:**

This document describes the `$in` query operator. For the `$in` aggregation operator, see [$in (expression operator).](https://www.mongodb.com/docs/manual/reference/operator/aggregation/in.md)

## Behavior

### Match an Array Value

If `field` has an array, the `$in` operator selects the documents whose `field` has an array that contains at least one element that matches a value in the specified array. For example, `<value1>`, `<value2>`, and so on.

### Performance

`$in` compares each parameter to each document in the collection, which can cause performance issues. To improve performance, create an index on the `field` you want to query. An index allows MongoDB to create bounds for each `$in` element and search more efficiently.

## Query Data on Atlas by Using MongoDB Search

For data stored in [MongoDB Atlas](https://www.mongodb.com/docs/atlas/), you can use the [MongoDB Search](https://www.mongodb.com/docs/atlas/atlas-search/) [in Operator](https://www.mongodb.com/docs/search/query/operators-collectors/in.md#std-label-in-ref) operator when running [`$search`](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search) queries. Running `$in` after [`$search`](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search) is less performant than running [`$search`](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search) with the [in Operator](https://www.mongodb.com/docs/search/query/operators-collectors/in.md#std-label-in-ref) operator.

To learn more about the MongoDB Search version of this operator, see the [in Operator](https://www.mongodb.com/docs/search/query/operators-collectors/in.md#std-label-in-ref) operator in the Atlas documentation.

## Examples

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

### Match Values

This query selects documents in the `movies` collection where the value of the `rated` field is `"G"` or `"TV-G"`:

```javascript
db.movies.find(
   { rated: { $in: ["G", "TV-G"] } },
   { _id: 0, title: 1 }
)

```

**Output:**

```text
[
  { title: 'The Great Train Robbery' },
  { title: 'A Corner in Wheat' },
  { title: 'From Hand to Mouth' },
  { title: 'One Week' },
  { title: 'The Devil to Pay!' },
  { title: 'Footlight Parade' },
  { title: 'Gold Diggers of 1935' },
  { title: 'Naughty Marietta' },
  { title: 'Modern Times' },
  { title: 'Gone with the Wind' },
  { title: 'Fantasia' },
  { title: 'The Man Who Came to Dinner' },
  { title: 'National Velvet' },
  { title: 'Alice in Wonderland' },
  { title: 'The Member of the Wedding' },
  { title: 'Seven Brides for Seven Brothers' },
  { title: 'Around the World in Eighty Days' },
  { title: 'The King and I' },
  { title: 'A King in New York' },
  { title: 'Ben-Hur' }
]

```

Although you can write the query using the [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) operator, use the `$in` operator rather than the [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) operator when performing equality checks on the same field.

### Match Values in an Array

The following [`updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updatemany.md#mongodb-method-db.collection.updateMany) operation sets the `familyFriendly` field to `true` when the `rated` array has at least one element that matches either `"G"` or `"TV-G"`:

```javascript
db.movies.updateMany(
   { rated: { $in: ["G", "TV-G"] } },
   { $set: { familyFriendly: true } }
)

```

**Output:**

```text
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 536,
  modifiedCount: 536,
  upsertedCount: 0
}

```

For additional examples on querying arrays, see:

- [Query an Array](https://www.mongodb.com/docs/manual/tutorial/query-arrays.md#std-label-read-operations-arrays)

- [Query an Array of Embedded Documents](https://www.mongodb.com/docs/manual/tutorial/query-array-of-documents.md#std-label-array-match-embedded-documents)

For additional examples on querying, see [Read Documents.](https://www.mongodb.com/docs/manual/tutorial/query-documents.md#std-label-read-operations-query-document)

### Use `$in` with a Regular Expression

The `$in` operator can select documents using regular expressions of the form `/pattern/`.

This query selects documents in the `movies` collection where the `plot` field either starts with `Alien` or contains `sci-fi`:

```javascript
db.movies.find( 
   { plot: { $in: [ /^Alien/ , /sci-fi/ ] } },
   { _id: 0, title: 1, plot: 1 }
)

```

**Output:**

```text
[                                                                                                                                                                                                                                                                               
  {                                                                                                                                                                                                                                                                             
    plot: 'Aliens come to Earth seeking scientists to help them in their war.',                                                                                                                                                                                                 
    title: 'This Island Earth'                                                                                                                                                                                                                                                  
  },                                                                                                                                                                                                                                                                            
  {                                                                 
    plot: 'Censored by the Polish authorities, this film was reedited and new footage added. It begins with a sci-fi motif: abstract images and electronic music take the viewer from ruins of Lebanon ...',                                                                    
    title: 'Rece do gèry'                                           
  },                                                                
  {                                                                 
    plot: 'An idyllic sci-fi future has one major drawback: life must end at 30.',                                                      
    title: "Logan's Run"                                            
  },                                                                
  {                                                                 
    plot: "Four horror/sci-fi segments directed by four famous directors which are their own versions of classic stories from Rod Serling's landmark television series.",                                                                                                       
    title: 'Twilight Zone: The Movie'                               
  },                                                                
  {                                                                 
    plot: 'Aliens who look like clowns come from outer space and terrorize a small town.',                                              
    title: 'Killer Klowns from Outer Space'                         
  },
  ...
]


```

## Learn More

- [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find)

- [`updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updatemany.md#mongodb-method-db.collection.updateMany)

- [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or)

- [`$set`](https://www.mongodb.com/docs/manual/reference/operator/update/set.md#mongodb-update-up.-set)

- [`$elemMatch`](https://www.mongodb.com/docs/manual/reference/operator/query/elemmatch.md#mongodb-query-op.-elemMatch)
