【问题标题】:Using Aggregate to rename a field in MongoDB使用聚合重命名 MongoDB 中的字段
【发布时间】:2021-04-21 19:04:03
【问题描述】:

处理查询以将字段“专业知识”重命名为技能,专业知识是一个包含多个数组的数组,因此希望将其分割为 1。 表格示例:{ "_id" : "E08", "name" : "Damien Collins", "expertise" : [ "Python", "Java" ] }

希望将命名的专业知识显示为“技能”,并且只是为了展示一项专业知识。

当前查询:

db.employees.aggregate([{expertise:{$exists:true}},{$project:{_id:1,"Skill":{expertise{$slice:1}},name:1}}])

在技能部分重命名之前可以使用。

【问题讨论】:

    标签: sql mongodb


    【解决方案1】:

    使用$arrayElemAt返回数组的1个元素:

    {$project:{_id:1,"Skill":{$arrayElemAt:["$expertise",0]},name:1}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 2023-03-22
      • 2015-04-01
      • 2021-08-01
      相关资源
      最近更新 更多