ต่อไปนี้เป็นรูปแบบการรับข้อมูลที่ฝังอยู่ในเอกสาร MongoDB
db.yourCollectionName.find({},{‘yourOuterKeyName.yourInnerKeyName:1}).pretty(); ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน
> db.embeddedCollectionDemo.insertOne(
... {
... "StudentName" : "Larry",
... "StudentDetails": {
... "Larry1234": {"ProjectName": "Student Web Tracker"},
... "Larry7645": {"ProjectName": "Hospital Management System"},
... "Larry9879": {"ProjectName": "Library Management System"},
...
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5c98a100330fd0aa0d2fe4c5")
} ต่อไปนี้เป็นแบบสอบถามเพื่อแสดงเอกสารทั้งหมดจากคอลเลกชัน
> db.embeddedCollectionDemo.find().pretty();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"_id" : ObjectId("5c98a100330fd0aa0d2fe4c5"),
"StudentName" : "Larry",
"StudentDetails" : {
"Larry1234" : {
"ProjectName" : "Student Web Tracker"
},
"Larry7645" : {
"ProjectName" : "Hospital Management System"
},
"Larry9879" : {
"ProjectName" : "Library Management System"
}
}
} ต่อไปนี้เป็นแบบสอบถามสำหรับคอลเลกชันที่ฝัง เช่น ข้อมูลที่ฝังตัวในคอลเลกชัน MongoDB
> db.embeddedCollectionDemo.find({},{'StudentDetails.Larry7645':1}).pretty(); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"_id" : ObjectId("5c98a100330fd0aa0d2fe4c5"),
"StudentDetails" : {
"Larry7645" : {
"ProjectName" : "Hospital Management System"
}
}
}