我正在嘗試找出適合我當前情況的查詢。我有 Mongo db 檔案,看起來像這樣
{
"_id" : ObjectId("627e24df17446e1f646c945c"),
"phone_number" : "041041041",
"country" : "_si",
"blacklisted_service" : {
"all" : false,
"place_order" : false,
"promo_code_sms" : true
},
}
現在我正在嘗試查詢檔案,其中"phone_number": 041041041&& "country" : "_si"AND ( "blacklisted_service.all": trueOR "blacklisted_service.promo_code_sms": true)...
目前我正在嘗試使用聚合和 $elemMatch 函式來實作我的目標,但我的結果總是空的
db.getCollection("blacklisted_sms").aggregate([
{
$match: {
phone_number: '041041041',
country: '_si',
blacklisted_service: {
$elemMatch: {
'all': true,
$or: [
{
promo_code_sms: true,
}
]
},
}
}
}
]);
您能否幫我定義正確的查詢,如果 phone_number 和國家/地區匹配,除了檢查一個 blacklisted_service 鍵值是否為真之外,它將回傳一個結果。如果您有任何其他問題,請告訴我,我會提供。我正在使用 mongo DB 版本 4.x。先感謝您
uj5u.com熱心網友回復:
您可以嘗試以下,
db.collection.aggregate([
{
$match: {
$expr: {
$and: [
{ $eq: ["$phone_number", "041041041" ] },
{ $eq: [ "$country", "_si" ] },
{
$or: [
{ $eq: [ "$blacklisted_service.all", true ] },
{ $eq: [ "$blacklisted_service.promo_code_sms", true ] },
]
}
]
}
}
}
])
作業Mongo游樂場
uj5u.com熱心網友回復:
db.collection.aggregate([
{
$match: {
$and: [
{
"phone_number": "041041041"
},
{
"country": "_si"
},
{
$or: [
{
"blacklisted_service.all": true
},
{
"blacklisted_service.promo_code_sms": true
}
]
}
]
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/477714.html
標籤:mongodb
