let departmentsMap = {
'1576996323453': 'QA',
'1874996373493': 'Dev',
'1374990372493': 'BA',
'1874926373494': 'Tech Support',
}
上面是我的示例部门地图对象。我正在员工集合上运行聚合管道,其中每个文档都有一个部门字段,其值是相应部门的 id(上面的项目中的键)。现在我想按如下方式进行投影
{
$project:{
department : // i want get the department name here. data is there in local variable
}
}
立即提供帮助将不胜感激
您可以使用 $switch 来实现。
假设文档中的字段名称是“department”,则可能如下所示:
{$project: {
department: { $switch: {
branches: [
{case: {$eq: ["$department", "1576996323453"]}, then: "QA"}
{case: {$eq: ["$department", "1874996373493"]}, then: "Dev"}
{case: {$eq: ["$department", "1374990372493"]}, then: "BA"}
{case: {$eq: ["$department", "1874926373494"]}, then: "Tech Support"}
],
default: "$department"
}}