Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Query an Array

On this page

  • Match an Array
  • Query an Array for an Element
  • Specify Multiple Conditions for Array Elements
  • Query an Array with MongoDB Atlas
  • Additional Query Tutorials

You can query arrays in MongoDB using the following methods:

  • Your programming language's driver.

  • The MongoDB Atlas UI. To learn more, see Query an Array with MongoDB Atlas.

  • MongoDB Compass.


Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.


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.

The example in this section uses the sample movies dataset. To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.

To query an array in MongoDB Atlas, follow these steps:

1
  1. In the MongoDB Atlas UI, click Database in the sidebar.

  2. For the database deployment that contains the sample data, click Browse Collections.

  3. In the left navigation pane, select the sample_mflix database.

  4. Select the movies collection.

2

To query a document that contains an array, specify a query filter document. A query filter document uses query operators to specify search conditions. Use the following example documents to query array fields in the sample_mflix.movies collection.

To apply a query filter, copy an example document into the Filter search bar and click Apply.

For additional query examples, see:

←  Query on Embedded/Nested DocumentsQuery an Array of Embedded Documents →