เพื่อดึงเอกสารเฉพาะ ให้ใช้สัญลักษณ์จุด (dot notation) ในเมธอด find() ของ MongoDB มาสร้างคอลเลกชันพร้อมเอกสารกันก่อน
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Samsung"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"OnePlus"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }
แสดงเอกสารทั้งหมดจากคอลเลกชันด้วยเมธอด find()
> db.demo672.find();
จะได้ผลลัพธ์ดังนี้
{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] } { "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Samsung" } ] } { "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }
คำสั่งสำหรับดึงเอกสารเฉพาะ
> db.demo672.find({"Brand.Name":"OnePlus"});
จะได้ผลลัพธ์ดังนี้
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }