ใช้ Dot Notation Query ฟิลด์ซ้อนใน Object และ Array
MongoDB รองรับการ Query เข้าถึงฟิลด์ลึก (Nested Field) โดยใช้ Dot Notation (จุด) คั่นระหว่างระดับ
ตัวอย่างข้อมูล: Customer มี Details เป็น Array of Objects
> db.multipleParametersDemo.insertOne({
"CustomerName": "Larry",
"CustomerDetails": [
{
"CustomerCountryName": "US",
"CustomerBankName": "HDFC",
"CustomerBalance": 17363
}
],
"Purchase": 1456
});
คิวรี: หา CustomerName = "Larry" AND CustomerDetails.CustomerCountryName = "US"
> db.multipleParametersDemo.find({
CustomerName: 'Larry',
'CustomerDetails.CustomerCountryName': 'US'
}).count();
ผลลัพธ์
1
Dot Notation Patterns
| Pattern | ตัวอย่าง | Match กรณีไหน |
|---|---|---|
| Object.Field | { "address.city": "Bangkok" } | Object ตรงๆ |
| Array.Object.Field | { "orders.items.name": "iPhone" } | Element ใด Element หนึ่งใน Array Match |
| Array.Index.Field | { "tags.0": "important" } | Element ที่ Index 0 |
| Deep Nesting | { "a.b.c.d": 123 } | หลายชั้น |