ใช้ $elemMatch สำหรับการกรองหลายเงื่อนไข
ตัวดำเนินการ $elemMatch ใช้จับคู่เอกสารที่มีฟิลด์อาร์เรย์ซึ่งมีอิลิเมนต์อย่างน้อยหนึ่งตัวตรงกับเกณฑ์ที่กำหนดทั้งหมด
ตัวอย่างข้อมูลนักเรียน
> db.filterBySeveralElementsDemo.insertOne({ "_id":100, "StudentDetails": [ { "StudentName": "John", "StudentCountryName": "US" }, { "StudentName": "Carol", "StudentCountryName": "UK" } ] });
คำสั่งกรองข้อมูลที่ StudentName="Sam" และ StudentCountryName="AUS":
> db.filterBySeveralElementsDemo.find({ StudentDetails: { $elemMatch: { StudentName: 'Sam', StudentCountryName: 'AUS' }}}).pretty();
ผลลัพธ์จะคืนเฉพาะเอกสารที่มีอิลิเมนต์ตรงกับเงื่อนไขทั้งสองพร้อมกัน