如果条件节点为js,则更改值聚合



如何更改节点js-mongodb 中的agggte

我有$project:

$project : {
type: '$data.type',
}

它给我输出:

'type' : [
'bold',
'bold',
'regular',
'bold',
'regular'
]

我想更改输出

如果粗体,则为厚,否则为薄

我试着使用$cond$switch,但我想知道为什么我只得到1个输出,这是默认的输出

type: {
$switch : {
branches : [ 
{
case : { $eq : ['$data.type', 'bold']}, then: 'thick'
},
{
case : { $eq : ['$data.type', 'regular']}, then: 'thin'
},
],

default: ""
}
},

输出:

"type": "",

你们能帮我解决这个问题吗?我也试过$cond$eq,但输出仍然是默认输出

感谢您的帮助

$map有助于修改或遍历所有数据。$cond有助于为数据添加条件

db.collection.aggregate([
{
$project: {
type: {
$map: {
input: "$type",
in: {
$cond: [{$eq: ["$$this","bold"]},
"thick",
"thin"
]
}
}
}
}
}
])

正在工作的Mongo游乐场

最新更新