- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reach subdata in MongoDB and display a particular document?
In order to reach subdata, you need to use key in MongoDB. Let us create a collection with documents −
>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris","StudentAge":21}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e7b590e71f552a0ebb0a6e6") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"David","StudentAge":23}}});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b591a71f552a0ebb0a6e7") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike","StudentAge":22}}});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b592271f552a0ebb0a6e8") }
Display all documents from a collection with the help of find() method −
> db.demo450.find();
This will produce the following output −
{ "_id" : ObjectId("5e7b590e71f552a0ebb0a6e6"), "Information" : { "StudentDetails" : { "StudentName" : "Chris", "StudentAge" : 21 } } } { "_id" : ObjectId("5e7b591a71f552a0ebb0a6e7"), "Information" : { "StudentDetails" : { "StudentName" : "David", "StudentAge" : 23 } } } { "_id" : ObjectId("5e7b592271f552a0ebb0a6e8"), "Information" : { "StudentDetails" : { "StudentName" : "Mike", "StudentAge" : 22 } } }
Following is the query to reach subdata in MongoDB −
> db.demo450.find({"Information.StudentDetails.StudentName":"David"});
This will produce the following output −
{ "_id" : ObjectId("5e7b591a71f552a0ebb0a6e7"), "Information" : { "StudentDetails" : { "StudentName" : "David", "StudentAge" : 23 } } }
Advertisements
To Continue Learning Please Login
Login with Google