How to create double nested array in MongoDB?

MongoDBBig Data AnalyticsDatabase

To create a double nested array in MongoDB, let us implement the query to create a collection with documents. Within that, we have created a double nested array that displays Student Details, with the project name and technologies used to develop the same project:

> db.doubleNestedArrayDemo.insertOne(
... {
...    "StudentId" : "1000",
...    "StudentName" : "Larry",
...    "StudentDetails" : [
...    {
...       "ProjectName" : "Online Banking",
...       "ProjectDetails" : [
...       {
...          "TechnologyUsed" : "Java"
...       },
...       {
...          "TechnologyUsed" : "MySQL in Backend"
...       }
...       ]
...    }
...    ]
... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c992bd7330fd0aa0d2fe4cc")
}
> db.doubleNestedArrayDemo.insertOne( { "StudentId" : "1001", "StudentName" : "Robert", "StudentDetails" : [ { "ProjectName" : "Student Web Tracker", "ProjectDetails" : [ { "TechnologyUsed" : "Django Framework" }, { "TechnologyUsed" : "MongoDB in Backend" } ] } ] } );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c992cdb330fd0aa0d2fe4cd")
}

Following is the query to display all the documents from a collection with the help of find() method

> db.doubleNestedArrayDemo.find().pretty()

This will produce the following output

{
   "_id" : ObjectId("5c992bd7330fd0aa0d2fe4cc"),
   "StudentId" : "1000",
   "StudentName" : "Larry",
   "StudentDetails" : [
      {
         "ProjectName" : "Online Banking",
         "ProjectDetails" : [
            {
               "TechnologyUsed" : "Java"
            },
            {
               "TechnologyUsed" : "MySQL in Backend"
            }
         ]
      }
   ]
}
{
   "_id" : ObjectId("5c992cdb330fd0aa0d2fe4cd"),
   "StudentId" : "1001",
   "StudentName" : "Robert",
   "StudentDetails" : [
      {
         "ProjectName" : "Student Web Tracker",
         "ProjectDetails" : [
            {
               "TechnologyUsed" : "Django Framework"
            },
            {
               "TechnologyUsed" : "MongoDB in Backend"
            }
         ]
      }
   ]
}
raja
Published on 11-Apr-2019 12:05:05
Advertisements