Docs Home → MongoDB Manual
On this page
- Match an Array
- Query an Array for an Element
- Specify Multiple Conditions for Array Elements
- Additional Query Tutorials
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.
The following example queries for all documents where the field tags
value is an array with exactly two elements, "red"
and "blank"
, in the specified order:
If, instead, you wish to find an array that contains both the elements "red"
and "blank"
, without regard to order or other elements in the array, use the $all
operator:
The following example queries for all documents where tags
is an array that contains the string "red"
as one of its elements:
For example, the following operation
queries for all documents where the array dim_cm
contains at least one element whose value is greater than 25
.
When specifying compound conditions on array elements, you can specify the query such that either a single array element meets these condition or any combination of array elements meets the conditions.
The following example queries for documents where the dim_cm
array contains elements that in some combination satisfy the query
conditions; e.g., one element can satisfy the greater than 15
condition and another element can satisfy the less than 20
condition, or a single element can satisfy both:
Use $elemMatch
operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.
The
following example queries for documents where the dim_cm
array contains at least one element that is both greater than [$gt
] 22
and less than [$lt
] 30
:
Using dot notation, you can specify query conditions for an element at a particular index or position of the array. The array uses zero-based indexing.
Note
When querying using dot notation, the field and nested field must be inside quotation marks.
The following example queries for all documents where the second element in the
array dim_cm
is greater than 25
:
Use the $size
operator to query for arrays by number of elements. For example, the following selects documents where the array tags
has 3 elements.
For additional query examples, see:
Query Documents
Query on Embedded/Nested Documents
Query an Array of Embedded Documents
Docs Home → MongoDB Manual
$in
The
$in
operator selects the documents where the value of a field equals any value in the specified array. To specify an $in
expression, use the following prototype:
{ field: { $in: [, , ... ] } }
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, ,
, 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].
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 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:
Query an Array
Query an Array of Embedded Documents
For additional examples in querying, see:
Query Documents
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
.