Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

MongoDB ค้นหา () แบบสอบถามสำหรับเอกสารที่ซ้อนกัน?


หากต้องการดึงค่าจากเอกสารที่ซ้อนกัน ให้ใช้เครื่องหมายจุด ให้เราสร้างคอลเลกชันพร้อมเอกสาร -

> db.demo591.insert([
...    { "Name": "John", "Age": 23 },
...    {"Name": "Carol", "Age": 26},
...    { "Name": "Robert", "Age": 29,
...    details:[
...       {
...          Email:"Robert@gmail.com",CountryName:"US"},{"Post":35}
...       ]}
... ]);
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -

> db.demo591.find();

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd3"), "Name" : "John", "Age" : 23 }
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd4"), "Name" : "Carol", "Age" : 26 }
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [ {
"Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 } ] }

ต่อไปนี้เป็นแบบสอบถามเพื่อดึงเอกสารที่ซ้อนกันโดยใช้เครื่องหมายจุด -

> db.demo591.find({"details.Email": "Robert@gmail.com"});

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [
   { "Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 } 
] }