
- 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
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 } } }
- Related Articles
- How to delete particular data in a document in MongoDB?
- Display collections in a particular MongoDB database?
- Display the undefined and exact MongoDB document records
- Display MongoDB with document and subdocument example and update
- MongoDB query to fetch a document that does not have a particular field?
- How do I get email-id from a MongoDB document and display with print()
- How to delete a document in MongoDB?
- MongoDB query select and display only a specific field from the document?
- How to get a particular anchor in a document in JavaScript?
- How to add a sub-document to sub-document array in MongoDB?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- How to get embedded data in a MongoDB document?
- How to insert a document with date in MongoDB?
- How to replace substring in MongoDB document?
- How to delete a MongoDB document using Java?

Advertisements