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