หากต้องการรับค่าภายในของข้อมูล JSON ให้ใช้ find() พร้อมกับเครื่องหมายจุด (.) ให้เราสร้างคอลเลกชันที่มีเอกสาร -
> db.demo235.insertOne(
... {
... "id":101,
... "details":[
... {
... "Name":"Chris Brown",
... "Age":21
... },
... {
... "Name":"David Miller",
... "Age":24
... }
... ],
... "otherdetails":[
... {
... "Score":56,
... "Subject":"MongoDB"
... },
... {
... "Score":78,
... "Subject":"MySQL"
... }
... ]
... }
...);
{
"acknowledged" : true,
"insertedId" : ObjectId("5e418d22f4cebbeaebec514b")
} แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo235.find().pretty();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{
"_id" : ObjectId("5e418d22f4cebbeaebec514b"),
"id" : 101,
"details" : [
{
"Name" : "Chris Brown",
"Age" : 21
},
{
"Name" : "David Miller",
"Age" : 24
}
],
"otherdetails" : [
{
"Score" : 56,
"Subject" : "MongoDB"
},
{
"Score" : 78,
"Subject" : "MySQL"
}
]
} ต่อไปนี้เป็นแบบสอบถามเพื่อรับค่าภายในของข้อมูล json -
> db.demo235.find({},{"otherdetails.Subject":1,_id:0}); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "otherdetails" : [ { "Subject" : "MongoDB" }, { "Subject" : "MySQL" } ] }