 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accessing inner element of JSON array in MongoDB?
To access inner element of JSON array in MongoDB, use dot notation. Let us create a collection with documents −
> db.demo687.insert({CountryName:'US', ... info: ... { ... id:101, ... details: ... [ ... { ...    Name:'Chris', ...    SubjectName:'MongoDB', ...    otherDetails:{ ...       "Marks":58, ...       Age:23 ...    } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo687.insert({CountryName:'UK', ... info: ... { ... id:102, ... details: ... [ ... { ...    Name:'David', ...    SubjectName:'MySQL', ...    otherDetails:{ ...       "Marks":78, ...       Age:21 ...    } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) Display all documents from a collection with the help of find() method −
> db.demo687.find();
This will produce the following output −
{ "_id" : ObjectId("5ea55658a7e81adc6a0b3962"), "CountryName" : "US", "info" : { "id" : 101, "details" : [ { "Name" : "Chris", "SubjectName" : "MongoDB", "otherDetails" : { "Marks" : 58, "Age" : 23 } } ] } } { "_id" : ObjectId("5ea55673a7e81adc6a0b3963"), "CountryName" : "UK", "info" : { "id" : 102, "details" : [ { "Name" : "David", "SubjectName" : "MySQL", "otherDetails" : { "Marks" : 78, "Age" : 21 } } ] } } Following is the query to access inner element of JSON array −
> db.demo687.find({"info.details.otherDetails.Marks":58}); This will produce the following output −
{ "_id" : ObjectId("5ea55658a7e81adc6a0b3962"), "CountryName" : "US", "info" : { "id" : 101, "details" : [ { "Name" : "Chris", "SubjectName" : "MongoDB", "otherDetails" : { "Marks" : 58, "Age" : 23 } } ] } }Advertisements
 