
- 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
Write a MongoDB query to get nested value?
You can use dot notation to get nested value. Let us first create a collection with documents
> db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "John", ... "EmployeeDetails" : ... { ... ... "_id":"EMP-101", ... "EmployeeAge":23, ... "EmployeeCompanyName":"IBM" ... ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea31dd628fa4220163b69") } > db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "Carol", ... "EmployeeDetails" : ... { ... ... "_id":"EMP-110", ... "EmployeeAge":29, ... "EmployeeCompanyName":"Amazon" ... ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea36bd628fa4220163b6a") }
Following is the query to display all documents from a collection with the help of find() method
> db.nestedQueryDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9ea31dd628fa4220163b69"), "EmployeeName" : "John", "EmployeeDetails" : { "_id" : "EMP-101", "EmployeeAge" : 23, "EmployeeCompanyName" : "IBM" } } { "_id" : ObjectId("5c9ea36bd628fa4220163b6a"), "EmployeeName" : "Carol", "EmployeeDetails" : { "_id" : "EMP-110", "EmployeeAge" : 29, "EmployeeCompanyName" : "Amazon" } }
Following is the query for MongoDB nested value query
> db.nestedQueryDemo.find({"EmployeeDetails._id":"EMP-110"}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9ea36bd628fa4220163b6a"), "EmployeeName" : "Carol", "EmployeeDetails" : { "_id" : "EMP-110", "EmployeeAge" : 29, "EmployeeCompanyName" : "Amazon" } }
- Related Articles
- MongoDB query to get array of nested string?
- MongoDB query to get only specific fields in nested array documents?
- Query MongoDB for a nested search
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to update nested document
- MongoDB query to update the nested document?
- Query deeply nested Objects in MongoDB
- MongoDB find() query for nested document?
- Get only the FALSE value with MongoDB query
- Increment MongoDB value inside a nested array
- Query a nested field within an array with MongoDB
- Query array of nested string with MongoDB?
- Group query upon nested object in MongoDB?
- Increment a value in a MongoDB nested object?

Advertisements