您好我正在嘗試在 TIBCO jasperstudio 中使用 MONGODB 查詢來創建報告
我要做的是使用兩個引數@orderitemuid 和@ordercatuid 過濾資料。我的情況是,如果我使用@orderitemuid 放置引數,它將忽略@ordercatuid 的引數。反之亦然,如果我使用@ordercatuid 放置引數,它將忽略@orderitemuid 的引數。但是在查詢中使用機器人引數時也有一個選項。我在 $match 中使用了 $switch,但出現錯誤。下面是我正在使用的 $match
{
$match: {
$switch: {
branches: [
{
case: { $eq: [{ $IfNull: [$P{orderitemuid}, 0] }, 0] },
then: { 'ordcat._id': { '$eq': { '$oid': $P{ordercatuid} } } },
},
{
case: { $eq: [{ $IfNull: [$P{ordercatuid}, 0] }, 0] },
then: { '_id': { '$eq': { '$oid': $P{orderitemuid} } } },
},
],
default: {
$expr: {
$and: [
{ $eq: ['_id', { '$oid': $P{orderitemuid} }] },
{ $eq: ['ordcat_id', { '$oid': $P{ordercatuid} }] },
],
},
},
},
},
}
先感謝您
uj5u.com熱心網友回復:
如$match 檔案中所述
$match 接受一個指定查詢條件的檔案。查詢語法與讀操作查詢語法相同;即$match 不接受原始聚合運算式。...
$switch是一個聚合運算式。這意味著它不能在$match沒有包裹的情況下在舞臺中使用$expr。
但是,您可以用 包裝它$expr,這還需要您稍微重構回傳值,如下所示:
db.collection.aggregate([
{
$match: {
$expr: {
$switch: {
branches: [
{
case: {
$eq: [
{
$ifNull: [
$P{orderitemuid},
0
]
},
0
]
},
then: {
$eq: [
"$ordcat._id",
{"$oid":$P{ordercatuid}}
]
}
},
{
case: {
$eq: [
{
"$ifNull": [
$P{ordercatuid},
0
]
},
0
]
},
then: {
$eq: [
"$_id",
{"$oid":$P{orderitemuid}}
]
}
}
],
default: {
$and: [
{
$eq: [
"$_id",
{"$oid": $P{orderitemuid} }
]
},
{
$eq: [
"$ordcat_id",
{"$oid": $P{ordercatuid}}
]
}
]
}
}
}
}
}
])
蒙戈游樂場
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414143.html
標籤:
