แยกองค์ประกอบเฉพาะออกจากอาร์เรย์ที่ซ้อนกันโดยใช้เครื่องหมายจุด (.) ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน -
> 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"
}
]
}
]
}
]
}