Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

การสร้าง JSON แบบลำดับชั้น (Hierarchical) ใน MongoDB


ใช้ไวยากรณ์ต่อไปนี้เพื่อสร้าง JSON แบบลำดับชั้นใน MongoDB

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