
- 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 only get the data of the nested JSON object in MongoDB?
To get the data of the nested JSON object in MongoDB, use findOne(). Let us create a collection with documents −
> db.demo109.insertOne( ... { ... "Name" : "Chris", ... "Subjects" : [ ... { ... "Id" : "100", ... "Name":"MySQL", ... "InstructorDetails" : [ ... { ... "Name" : "John" ... } ... ] ... }, ... { ... "Id" : "101", ... "Name":"MongoDB", ... "InstructorDetails" : [ ... { ... "Name" : "Mike" ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2ee7df9fd5fd66da21447a") }
Display all documents from a collection with the help of find() method −
> db.demo109.find();
This will produce the following output −
{ "_id" : ObjectId("5e2ee7df9fd5fd66da21447a"), "Name" : "Chris", "Subjects" : [ { "Id" : "100", "Name" : "MySQL", "InstructorDetails" : [ { "Name" : "John" } ] }, { "Id" : "101", "Name" : "MongoDB", "InstructorDetails" : [ { "Name" : "Mike" } ] } ] }
Following is the query to only get the data of the nested JSON object in MongoDB −
> db.demo109.findOne( ... { Name: "Chris"} ... , { 'Subjects': { $elemMatch:{'Id':"100"} } } ... , function (err, doc) { console.log(doc) });
This will produce the following output −
{ "_id" : ObjectId("5e2ee7df9fd5fd66da21447a"), "Subjects" : [ { "Id" : "100", "Name" : "MySQL", "InstructorDetails" : [ { "Name" : "John" } ] } ] }
- Related Articles
- Get value for key from nested JSON object in JavaScript
- MongoDB query to get only specific fields in nested array documents?
- How to display only the keys from nested MongoDB documents?
- Is there any way in MongoDB to get the inner value of json data?
- Create nested JSON object in PHP?
- Print JSON nested object in JavaScript?
- How to get the size of a json object in JavaScript?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to retrieve a nested object in MongoDB?
- How to get the size of an array within a nested JSON in Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?
- Constructing a nested JSON object in JavaScript
- Retrieve values from nested JSON array in MongoDB?
- How can we parse a nested JSON object in Java?
- How to get the values of the different types from a JSON object in Java?

Advertisements