![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
Is there any way in MongoDB to get the inner value of json data?
To get inner value of JSON data, use find() along with dot(.) notation. Let us create a collection with documents −
> db.demo235.insertOne( ... { ... "id":101, ... "details":[ ... { ... "Name":"Chris Brown", ... "Age":21 ... }, ... { ... "Name":"David Miller", ... "Age":24 ... } ... ], ... "otherdetails":[ ... { ... "Score":56, ... "Subject":"MongoDB" ... }, ... { ... "Score":78, ... "Subject":"MySQL" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e418d22f4cebbeaebec514b") }
Display all documents from a collection with the help of find() method −
> db.demo235.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e418d22f4cebbeaebec514b"), "id" : 101, "details" : [ { "Name" : "Chris Brown", "Age" : 21 }, { "Name" : "David Miller", "Age" : 24 } ], "otherdetails" : [ { "Score" : 56, "Subject" : "MongoDB" }, { "Score" : 78, "Subject" : "MySQL" } ] }
Following is the query to get the inner value of json data −
> db.demo235.find({},{"otherdetails.Subject":1,_id:0});
This will produce the following output −
{ "otherdetails" : [ { "Subject" : "MongoDB" }, { "Subject" : "MySQL" } ] }
Advertisements