For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Query on Embedded/Nested Documents

Query embedded documents in MongoDBwith the following methods:


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


This page provides examples of query operations on embedded/nested documents using the db.collection.find() method in mongosh.

db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" }, { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }, { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }, { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }]);

Specify query conditions on fields in an embedded/nested document with dot notation "field.nestedField".

Note

When you query with dot notation, the field and nested field must be inside quotation marks.

The following example selects all documents where the field uom nested in the size field equals "in":

db.inventory.find( { "size.uom": "in" } )

A query filter document can use the query operators to specify conditions in the following form:

{ <field1>: { <operator1>: <value1> }, ... }

The following query uses the less than operator ($lt) on the field h embedded in the size field:

db.inventory.find( { "size.h": { $lt: 15 } } )

The following query selects all documents where the nested field h is less than 15, the nested field uom equals "in", and the status field equals "D":

db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )

To specify an equality condition on a field that is an embedded/nested document, use the query filter document { <field>: <value> } where <value> is the document to match.

For example, the following query selects all documents where the field size equals the document { h: 14, w: 21, uom: "cm" }:

db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )

Warning

MongoDB does not recommend comparisons on embedded documents because the operations require an exact match of the specified <value> document, including the field order.

For example, the following query does not match any documents in the inventory collection:

db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )

Queries that use comparisons on embedded documents can result in unpredictable behavior when used with a driver that does not use ordered data structures for expressing queries.

This example uses the sample movies dataset. To load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.

To query an embedded document in MongoDB Atlas, follow these steps:

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Clusters under the Database heading.

    The Clusters page displays.

2
  1. For the cluster that contains the sample data, click Browse Collections.

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

  3. Select the movies collection.

3

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:

{ "awards.wins": 1 }
4

This query filter returns all documents in the sample_mflix.movies collection where the embedded document for the awards field contains { wins: 1 }.

For additional query examples, see: