Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Query an Array of Embedded Documents

On this page

  • Query for a Document Nested in an Array
  • Specify a Query Condition on a Field in an Array of Documents
  • Specify Multiple Conditions for Array of Documents
  • Query an Array of Documents with MongoDB Atlas
  • Additional Query Tutorials

You can query documents in MongoDB by using the following methods:

  • Your programming language's driver.

  • The MongoDB Atlas UI. To learn more, see Query an Array of Documents 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 selects all documents where an element in the instock array matches the specified document:

Equality matches on the whole embedded/nested document require an exact match of the specified document, including the field order. For example, the following query does not match any documents in the inventory collection:

If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.

The following example selects all documents where the instock array has at least one embedded document that contains the field qty whose value is less than or equal to 20:

Using dot notation, you can specify query conditions for field in a document at a particular index or position of the array. The array uses zero-based indexing.

Note

When querying using dot notation, the field and index must be inside quotation marks.

The following example selects all documents where the instock array has as its first element a document that contains the field qty whose value is less than or equal to 20:

When specifying conditions on more than one field nested in an array of documents, you can specify the query such that either a single document meets these condition or any combination of documents (including a single document) in the array meets the conditions.

Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.

The following example queries for documents where the instock array has at least one embedded document that contains both the field qty equal to 5 and the field warehouse equal to A:

The following example queries for documents where the instock array has at least one embedded document that contains the field qty that is greater than 10 and less than or equal to 20:

If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.

For example, the following query matches documents where any document nested in the instock array has the qty field greater than 10 and any document (but not necessarily the same embedded document) in the array has the qty field less than or equal to 20:

The following example queries for documents where the instock array has at least one embedded document that contains the field qty equal to 5 and at least one embedded document (but not necessarily the same embedded document) that contains the field warehouse equal to A:

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

To query an array of documents 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_training database.

  4. Select the grades collection.

2

Specify the query filter document in the Filter field. A query filter document uses query operators to specify search conditions.

Copy the following query filter document into the Filter search bar:

{ scores: { type: "exam" } }
3

This query filter returns all documents in the sample_training.grades collection where the scores field contains an exam score type.

For additional query examples, see:

←  Query an ArrayProject Fields to Return from Query →