你可以在下面使用
$project
舞台使用
$cond
聚合运算符
{ "$project": {
"field1": { "$cond": [{ "$ne": ["$field1", null] }, { "$concat": ["$field1", "-"] }, ""] },
"field3": { "$cond": [{ "$eq": ["$field3", "ok"] }, "approved", "pending"] },
"field2": 1
}},
{ "$project": {
"fieldName": { "$concat": ["$field1", "$field2", "$field3"] }
}}
甚至在一个阶段
{ "$project": {
"fieldName": {
"$concat": [
{ "$cond": [{ "$ne": ["$field1", null] }, { "$concat": ["$field1", "-"] }, ""] },
"$field2",
{ "$cond": [{ "$eq": ["$field3", "ok"] }, "approved", "pending"] }
]
}
}}