dot(.) 표기법을 사용하여 중첩 배열에서 특정 요소를 추출합니다. 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> db.extractParticularElementDemo.insertOne(
... {
... "_id" : 101,
... "StudentName" : "John",
... "StudentInformation" : [
... {
... "Age" : 21,
... "StudentPersonalInformation" : [
... {
... "StudentNickName" : "Mike",
... "StudentFamilyDetails" : [
... {
... "FatherName" : "Carol"
... }
... ]
... },
... {
... "StudentAnotherName" : "David",
... "StudentFamilyDetails" : [
... {
... "FatherName" : "Robert"
... }
... ]
... }
... ]
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 101 } 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.extractParticularElementDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : 101,
"StudentName" : "John",
"StudentInformation" : [
{
"Age" : 21,
"StudentPersonalInformation" : [
{
"StudentNickName" : "Mike",
"StudentFamilyDetails" : [
{
"FatherName" : "Carol"
}
]
},
{
"StudentAnotherName" : "David",
"StudentFamilyDetails" : [
{
"FatherName" : "Robert"
}
]
}
]
}
]
} 다음은 중첩 배열에서 특정 요소를 추출하는 쿼리입니다 -
> db.extractParticularElementDemo.find(
... {'StudentInformation.StudentPersonalInformation.StudentFamilyDetails.FatherName':'Carol'},
... {'StudentInformation.StudentPersonalInformation.StudentFamilyDetails.FatherName':1,"_id":0}
... ).pretty(); 이것은 다음과 같은 출력을 생성합니다 -
{
"StudentInformation" : [
{
"StudentPersonalInformation" : [
{
"StudentFamilyDetails" : [
{
}
"FatherName" : "Carol"
]
},
{
"StudentFamilyDetails" : [
{
"FatherName" : "Robert"
}
]
}
]
}
]
}