คุณสามารถใช้ตัวดำเนินการ $where ร่วมกับสคริปต์บางตัวได้
ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน -
> dbsameValueMultipleTimesDemoinsertOne(
{
"ListOfPrice":[
{"Price": 110},
{"Price":130},
{"Price": 145}
]
}
);
{
"acknowledged" : true,
"insertedId" : ObjectId("5cefc4e6ef71edecf6a1f6b9")
}
> dbsameValueMultipleTimesDemoinsertOne(
{
"ListOfPrice":[
{"Price": 110},
{"Price":178},
{"Price": 110}
]
}
);
{
"acknowledged" : true,
"insertedId" : ObjectId("5cefc4e7ef71edecf6a1f6ba")
} ต่อไปนี้เป็นแบบสอบถามเพื่อแสดงเอกสารทั้งหมดจากคอลเลกชันโดยใช้วิธี find() -
> dbsameValueMultipleTimesDemofind()pretty();
ผลลัพธ์
{
"_id" : ObjectId("5cefc4e6ef71edecf6a1f6b9"),
"ListOfPrice" : [
{
"Price" : 110
},
{
"Price" : 130
},
{
"Price" : 145
}
]
}
{
"_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"),
"ListOfPrice" : [
{
"Price" : 110
},
{
"Price" : 178
},
{
"Price" : 110
}
]
} ต่อไปนี้เป็นแบบสอบถามเพื่อค้นหาค่าเดียวกันหลายครั้งในอาร์เรย์ -
> dbsameValueMultipleTimesDemofind({
"$where": function() {
return thisListOfPricefilter(function(p) {
return pPrice == 110;
})length > 1;
}
}) ผลลัพธ์
{ "_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"), "ListOfPrice" : [ { "Price" : 110 }, { "Price" : 178 }, { "Price" : 110 } ] }