
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB find() query for nested document?
To fetch a value from the nested document, use dot notation. Let us create a collection with documents −
> db.demo591.insert([ ... { "Name": "John", "Age": 23 }, ... {"Name": "Carol", "Age": 26}, ... { "Name": "Robert", "Age": 29, ... details:[ ... { ... Email:"Robert@gmail.com",CountryName:"US"},{"Post":35} ... ]} ... ]); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Display all documents from a collection with the help of find() method −
> db.demo591.find();
This will produce the following output −
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd3"), "Name" : "John", "Age" : 23 } { "_id" : ObjectId("5e92dd08fd2d90c177b5bcd4"), "Name" : "Carol", "Age" : 26 } { "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [ { "Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 } ] }
Following is the query to fetch nested document using dot notation −
> db.demo591.find({"details.Email": "Robert@gmail.com"});
This will produce the following output −
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [ { "Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 } ] }
- Related Articles
- MongoDB query to update nested document
- MongoDB query to update the nested document?
- Query MongoDB for a nested search
- Updating nested document in MongoDB
- MongoDB query for fields in embedded document?
- Set condition in MongoDB nested document?
- Perform nested document value search in MongoDB?
- MongoDB query for exact match on multiple document fields
- Query deeply nested Objects in MongoDB
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- Query array of nested string with MongoDB?
- Group query upon nested object in MongoDB?
- MongoDB query to return only embedded document?
- MongoDB query to remove a specific document

Advertisements