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

ค้นหาเอกสาร MongoDB จาก Sub-array ด้วย Dot Notation และ $in

ใช้ Dot Notation เข้าถึงฟิลด์ภายใน Object ที่อยู่ใน Array (Sub-array) ร่วมกับ Operator $in เพื่อค้นหาเอกสารที่มี Element ใน Array ตรงกับเงื่อนไขหลายค่า

สร้างคอลเลกชันและข้อมูลตัวอย่าง (Array of Objects)

> db.findDocumentDemo.insertOne({
    "EmployeeDetails": {
      "EmployeeAppraisalTime": [
        {"EmployeeDesignation": "Developer", "Salary": 45000},
        {"EmployeeDesignation": "Tester", "Salary": 30000},
        {"EmployeeDesignation": "HR", "Salary": 22000},
        {"EmployeeDesignation": "Accountant", "Salary": 18000}
      ]
    }
  });
{ "acknowledged": true, "insertedId": ObjectId("5cd2c0f7b64f4b851c3a13a8") }

> db.findDocumentDemo.insertOne({
    "EmployeeDetails": {
      "EmployeeAppraisalTime": [
        {"EmployeeDesignation": "Developer", "Salary": 105000},
        {"EmployeeDesignation": "Tester", "Salary": 45000},
        {"EmployeeDesignation": "HR", "Salary": 34000},
        {"EmployeeDesignation": "Accountant", "Salary": 24000}
      ]
    }
  });
{ "acknowledged": true, "insertedId": ObjectId("5cd2c1d5b64f4b851c3a13a9") }

แสดงข้อมูลทั้งหมด

> db.findDocumentDemo.find().pretty();

ผลลัพธ์: 2 เอกสาร แต่ละอันมี Array EmployeeAppraisalTime 4 Elements

ค้นหาเอกสารที่มี EmployeeDesignation = "Developer" และ Salary เป็น 45000 หรือ 105000

> db.findDocumentDemo.find({
  'EmployeeDetails.EmployeeAppraisalTime.EmployeeDesignation': 'Developer',
  'EmployeeDetails.EmployeeAppraisalTime.Salary': {$in: [45000, 105000]}
});

ผลลัพธ์: พบทั้ง 2 เอกสาร (ทั้งคู่มี Developer ที่ Salary ตรงเงื่อนไข)

{
  "_id": ObjectId("5cd2c0f7b64f4b851c3a13a8"),
  "EmployeeDetails": {
    "EmployeeAppraisalTime": [
      {"EmployeeDesignation": "Developer", "Salary": 45000},
      {"EmployeeDesignation": "Tester", "Salary": 30000},
      {"EmployeeDesignation": "HR", "Salary": 22000},
      {"EmployeeDesignation": "Accountant", "Salary": 18000}
    ]
  }
}
{
  "_id": ObjectId("5cd2c1d5b64f4b851c3a13a9"),
  "EmployeeDetails": {
    "EmployeeAppraisalTime": [
      {"EmployeeDesignation": "Developer", "Salary": 105000},
      {"EmployeeDesignation": "Tester", "Salary": 45000},
      {"EmployeeDesignation": "HR", "Salary": 34000},
      {"EmployeeDesignation": "Accountant", "Salary": 24000}
    ]
  }
}

การทำงานของ Query นี้

  • EmployeeDetails.EmployeeAppraisalTime.EmployeeDesignation: ตรวจสอบว่า Array มี Element ใดที่ Designation = "Developer"
  • EmployeeDetails.EmployeeAppraisalTime.Salary with $in: ตรวจสอบว่า Array เดียวกันมี Element ที่ Salary อยู่ใน [45000, 105000]
  • สำคัญ: Query นี้เป็น Logical AND ระหว่างฟิลด์ แต่เป็น OR ภายใน Array เดียวกัน (Element คนละตัวก็ได้)

หากต้องการจับคู่ใน Object เดียวกัน (Same Element)

ใช้ $elemMatch:

db.findDocumentDemo.find({
  "EmployeeDetails.EmployeeAppraisalTime": {
    $elemMatch: {
      "EmployeeDesignation": "Developer",
      "Salary": {$in: [45000, 105000]}
    }
  }
});

ผลลัพธ์จะเหมือนกันในกรณีนี้ เพราะ Developer แต่ละเอกสารมี Salary ตรงเงื่อนไขอยู่แล้ว