การค้นหาเอกสารที่มี Field Name เฉพาะพร้อมค่าที่กำหนดใน MongoDB
ใช้ ออเปอเรเตอร์ $exists ร่วมกับ Dot Notation เพื่อค้นหาเอกสารที่มี Nested Field ตรงตามเงื่อนไข
ตัวอย่างการสร้างคอลเลกชันและเพิ่มข้อมูล (Nested Structure)
> db.findByFieldName.insertOne({
"Client": {
"ClientDetails": {
"ClientName": "Larry",
"ClientAge": 29
},
"ClientProjectDetails": {
"ProjectName": "Online Book Store",
"TeamSize": 10,
"TechnologyUsed": "Spring Boot"
}
}
});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9e93b2d628fa4220163b64") }
> db.findByFieldName.insertOne({
"Client": {
"ClientDetails": {
"ClientName": "Chris",
"ClientAge": 27
},
"ClientEducationDetails": {
"isEducated": true,
"CollegeName": "M.I.T."
}
}
});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9e9421d628fa4220163b65") }
แสดงข้อมูลทั้งหมด
> db.findByFieldName.find().pretty();
ผลลัพธ์ (โครงสร้างต่างกัน):
{
"_id" : ObjectId("5c9e93b2d628fa4220163b64"),
"Client" : {
"ClientDetails" : { "ClientName" : "Larry", "ClientAge" : 29 },
"ClientProjectDetails" : { "ProjectName" : "Online Book Store", "TeamSize" : 10, "TechnologyUsed" : "Spring Boot" }
}
}
{
"_id" : ObjectId("5c9e9421d628fa4220163b65"),
"Client" : {
"ClientDetails" : { "ClientName" : "Chris", "ClientAge" : 27 },
"ClientEducationDetails" : { "isEducated" : true, "CollegeName" : "M.I.T." }
}
}
ค้นหาเอกสารที่มี Field "ClientProjectDetails" (ไม่สนค่า)
> db.findByFieldName.find({ "Client.ClientProjectDetails": { $exists: true } }).pretty();
ผลลัพธ์ (พบเฉพาะ Larry ที่มี ClientProjectDetails):
{
"_id" : ObjectId("5c9e93b2d628fa4220163b64"),
"Client" : {
"ClientDetails" : { "ClientName" : "Larry", "ClientAge" : 29 },
"ClientProjectDetails" : { "ProjectName" : "Online Book Store", "TeamSize" : 10, "TechnologyUsed" : "Spring Boot" }
}
}
คำอธิบาย
- Dot Notation: "Client.ClientProjectDetails" : เข้าถึง Nested Field
- $exists: true : ตรวจสอบว่า Field นี้มีอยู่จริง (ไม่สนใจค่าภายใน)
- Schema Variation: เอกสารใน Collection เดียวกันอาจมีโครงสร้างต่างกันได้ (Schema-less)
ตัวอย่าง Query เพิ่มเติม
1. ค้นหาตามค่าเฉพาะใน Nested Field
// Project Name = "Online Book Store"
db.findByFieldName.find({ "Client.ClientProjectDetails.ProjectName": "Online Book Store" });
// TeamSize > 5
db.findByFieldName.find({ "Client.ClientProjectDetails.TeamSize": { $gt: 5 } });
2. ค้นหาเอกสารที่มี Field ไหน Field หนึ่ง (OR)
db.findByFieldName.find({
$or: [
{ "Client.ClientProjectDetails": { $exists: true } },
{ "Client.ClientEducationDetails": { $exists: true } }
]
});
3. ค้นหาเอกสารที่ขาด Field เฉพาะ
// ไม่มี ClientProjectDetails
db.findByFieldName.find({ "Client.ClientProjectDetails": { $exists: false } });
4. ใช้ $elemMatch สำหรับ Array of Objects
// ถ้า ClientProjects เป็น Array
db.collection.find({
"ClientProjects": {
$elemMatch: {
"ProjectName": "Online Book Store",
"TeamSize": { $gte: 5 }
}
}
});
Projection: ดึงเฉพาะ Nested Field ที่ต้องการ
> db.findByFieldName.find(
{ "Client.ClientProjectDetails": { $exists: true } },
{ "Client.ClientProjectDetails": 1, _id: 0 }
);
ผลลัพธ์:
{
"Client" : {
"ClientProjectDetails" : {
"ProjectName" : "Online Book Store",
"TeamSize" : 10,
"TechnologyUsed" : "Spring Boot"
}
}
}
Index สำหรับ Nested Field
// Single Field Index
db.findByFieldName.createIndex({ "Client.ClientProjectDetails.ProjectName": 1 });
// Compound Index
db.findByFieldName.createIndex({
"Client.ClientDetails.ClientName": 1,
"Client.ClientProjectDetails.TechnologyUsed": 1
});
Dot Notation + $exists เป็นเทคนิคสำคัญสำหรับจัดการ Schema-less Data ที่โครงสร้างไม่คงที่