MongoDB – Query Embedded Documents Using Mongo Shell

MongoDB provides you read operations to retrieve embedded/nested documents from the collection or query a collection for a embedded/nested document. You can perform read operations using the db.collection.find() method. This method selects or views embedded/nested documents of the collection and returns the cursor to the selected document.

Syntax: db.collection.find(filter, projection)
Parameters:

  • filter: It is an optional parameter. It specifies the selection filter with the help of query operators. And if you want to get all the documents present in the collection, then omit these parameters or pass an empty document in the method. The type of this parameter is a Document.
  • projection: It is an optional parameter. It specifies that only those fields return to the document that matches the given query filter. And if you want to get all the fields in the document, then omit this parameter. Learn more.

Return: This method returns a cursor to the documents that match the specified query criteria. When you use find() method, it returns documents, which means the method is actually returning the cursor to the documents.

Accessing embedded/nested documents –

In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.

Syntax:



"field.nestedField": value

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: Courses
Document: three documents that contain the details of the students in the form of field-value pairs.

Matching embedded/nested document –

In this example, we are retrieving the documents that exactly match the given embedded document.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Courses.find({name: {first: "Rohit"
                        middle: "Kumar"
                        last: "Singh"}}).pretty()

chevron_right


Select documents that match the nested field –

In this example, we are retrieving the documents that match the specified nested field.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Courses.find({"courseDetails.name": "Java Backend Development"}).pretty()

chevron_right


Select documents that match the nested field (Using query operators) –

In this example, we are retrieving the documents that match the nested field using the query operators. Here, in the query, we use $in operator. This operator is used to match any of the values specified in the given array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Courses.find({"name.first": {$in: ["Rohit", "Mohit"]}}).pretty()

chevron_right


Select documents that match the nested fields (Using AND condition) –

In this example, we are retrieving the documents that match the nested fields.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Courses.find({"courseDetails.name": "Sudo GATE 2020",
                 "name.first": "Mohit"}).pretty()

chevron_right


Getting the specified fields from the embedded/nested documents :

In this example, we are retrieving fields from the embedded/nested documents using projection.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Courses.find({branch: "CSE"}, {"name.first": 1
                                  "name.last": 1}).pretty()

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.