MongoDB 中创建分层JSON

使用以下语法在MongoDB中创建分层JSON-

db.demo716.insertOne(
   {
      yourFieldName1,
      yourFieldName2,
      .
      .
      N,
      "fieldName": {
         yourFieldName1,
         yourFieldName2,
         .
         .
         N,
         "fieldname":
         [
            {
               yourFieldName1,
               yourFieldName2,
               .
               .
               N
            }
         ]
      }
   }
);

让我们创建一个包含文档的集合-

> 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")
}

在find()方法的帮助下显示集合中的所有文档-

> db.demo716.find().pretty();

这将产生以下输出-

{
   "_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"
         }
      ]
   }
}