

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Creating hierarchical JSON in MongoDB?
Use the following syntax to create hierarchical JSON in MongoDB −
db.demo716.insertOne( { yourFieldName1, yourFieldName2, . . N, "fieldName": { yourFieldName1, yourFieldName2, . . N, "fieldname": [ { yourFieldName1, yourFieldName2, . . N } ] } } );
Let us create a collection with documents −
> db.demo716.insertOne( ... { ... "id": 101, ... "UserEmailId": "John@gmail.com", ... "UserPassword": "123456", ... "UserInformation": { ... "UserName": "Chris", ... "UserAge": 26, ... "UserCountryName": "US", ... "OtherInformation": ... [ ... { ... "TeacherName":"Robert", ... "SubjectName":"MongoDB", ... "CollegeName":"MIT" ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea9b1ff85324c2c98cc4c2f") }
Display all documents from a collection with the help of find() method −
> db.demo716.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ea9b1ff85324c2c98cc4c2f"), "id" : 101, "UserEmailId" : "John@gmail.com", "UserPassword" : "123456", "UserInformation" : { "UserName" : "Chris", "UserAge" : 26, "UserCountryName" : "US", "OtherInformation" : [ { "TeacherName" : "Robert", "SubjectName" : "MongoDB", "CollegeName" : "MIT" } ] } }
- Related Questions & Answers
- Creating views in MongoDB
- JavaScript creating an array from JSON data?
- MongoDB print JSON without whitespace i.e. unpretty JSON?
- Creating alias in a MongoDB query?
- Hierarchical Database Model
- Hierarchical Data Model
- Retrieve values from nested JSON array in MongoDB?
- Accessing inner element of JSON array in MongoDB?
- Hierarchical Inheritance in Dart Programming
- What is hierarchical routing?
- What are Hierarchical Methods?
- Creating an index on a nested MongoDB field?
- What is Hierarchical model in DBMS?
- C# Example for Hierarchical Inheritance
- What is Agglomerative Hierarchical Clustering?
Advertisements