ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน -
> db.demo194.insertOne( ... { ... "_id": 101, ... "details": { ... "otherDetails": { ... "List1": ["MongoDB", "MySQL"], ... "List2": ["Java"], ... "List3": ["MongoDB", "C"] ... } ... } ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo194.insertOne( {"_id": 102, "details": { "otherDetails": { "List1": ["Java", "C"], "List2": ["C++"], "List3": ["Python", "Spring"] } } } ); { "acknowledged" : true, "insertedId" : 102 }
แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo194.find();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" : 101, "details" : { "otherDetails" : { "List1" : [ "MongoDB", "MySQL" ], "List2" : [ "Java" ], "List3" : [ "MongoDB", "C" ] } } } { "_id" : 102, "details" : { "otherDetails" : { "List1" : [ "Java", "C" ], "List2" : [ "C++" ], "List3" : [ "Python", "Spring" ] } } }
นี่คือวิธีการสอบถามวัตถุของอาร์เรย์ -
> db.demo194.find({ "details.otherDetails.List1": "MongoDB" })
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" : 101, "details" : { "otherDetails" : { "List1" : [ "MongoDB", "MySQL" ], "List2" : [ "Java" ], "List3" : [ "MongoDB", "C" ] } } }