ในการค้นหาอาร์เรย์ที่ตรงกับค่าในลำดับที่ต่างกัน คุณสามารถใช้ตัวดำเนินการ $all ให้เราสร้างคอลเลกชันที่มีเอกสาร ต่อไปนี้เป็นแบบสอบถาม
>db.exactMatchArrayDemo.insertOne({"StudentName":"David","StudentAge":22,"StudentGameScores":[45,78,98]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c94702d6669774125246c")
}
>db.exactMatchArrayDemo.insertOne({"StudentName":"Chris","StudentAge":23,"StudentGameScores":[45,78]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c94a42d6669774125246d")
} ต่อไปนี้เป็นแบบสอบถามเพื่อแสดงเอกสารทั้งหมดจากคอลเลกชันโดยใช้วิธี find()
> db.exactMatchArrayDemo.find().pretty();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"_id" : ObjectId("5c9c94702d6669774125246c"),
"StudentName" : "David",
"StudentAge" : 22,
"StudentGameScores" : [
45,
78,
98
]
}
{
"_id" : ObjectId("5c9c94a42d6669774125246d"),
"StudentName" : "Chris",
"StudentAge" : 23,
"StudentGameScores" : [
45,
78
]
} ต่อไปนี้เป็นแบบสอบถามเพื่อค้นหาการจับคู่อาร์เรย์ที่ตรงกันทั้งหมด
> db.exactMatchArrayDemo.find({ "StudentGameScores": { "$size" : 2, "$all": [ 78, 45 ] } }).pretty(); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"_id" : ObjectId("5c9c94a42d6669774125246d"),
"StudentName" : "Chris",
"StudentAge" : 23,
"StudentGameScores" : [
45,
78
]
} ต่อไปนี้เป็นข้อความค้นหาเพื่อค้นหาการจับคู่อาร์เรย์แบบตรงทั้งหมด แต่ลำดับไม่สำคัญ เราได้กำหนดขนาดที่แตกต่างกันเพื่อรับฟิลด์ “StudentGameScores” ด้วยค่า 3 ค่า
> db.exactMatchArrayDemo.find({ "StudentGameScores": { "$size" : 3, "$all": [ 78, 45 ] } }).pretty(); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"_id" : ObjectId("5c9c94702d6669774125246c"),
"StudentName" : "David",
"StudentAge" : 22,
"StudentGameScores" : [
45,
78,
98
]
}