Bạn có thể sử dụng toán tử $ where cùng với một số tập lệnh.
Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> 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")
} Sau đây là truy vấn để hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> dbsameValueMultipleTimesDemofind()pretty();
Đầu ra
{
"_id" : ObjectId("5cefc4e6ef71edecf6a1f6b9"),
"ListOfPrice" : [
{
"Price" : 110
},
{
"Price" : 130
},
{
"Price" : 145
}
]
}
{
"_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"),
"ListOfPrice" : [
{
"Price" : 110
},
{
"Price" : 178
},
{
"Price" : 110
}
]
} Sau đây là truy vấn để tìm cùng một giá trị nhiều lần trong một mảng -
> dbsameValueMultipleTimesDemofind({
"$where": function() {
return thisListOfPricefilter(function(p) {
return pPrice == 110;
})length > 1;
}
}) Đầu ra
{ "_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"), "ListOfPrice" : [ { "Price" : 110 }, { "Price" : 178 }, { "Price" : 110 } ] }