使用聚合框架 MongoDB 从嵌套字段创建新字段

我的文件:

{
  currentRole: { title: "Engineer"}
}

期望输出

{
  role: "Engineer"
}

试过这个:

let query = mongoose.model('cvs').aggregate(
    [
        {
            $project: {
               "currentRole.title":1,
               "_id": 0
            }
        }
    ]
);

但这给出了:

  {
    "currentRole": {
      "title": "Engineer"
    }
  },

如何创建具有标题值的新字段?

菲利克斯

替换您的项目阶段以使用该字段的值,如下所示:

   {
        $project: {
           "role": "$currentRole.title",
           "_id": 0
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章