在Mongo DB聚合中使用数组

时间:2019-08-27 01:07:36

标签: arrays mongodb aggregation-framework

需要帮助将对象数组转换为简单数组列表

我的JSON文档中的一个字段如下-

"joinResults" : [
    {
        "value" : "Spouses"
    }, 
    {
        "value" : "Children"
    },  
    {
        "value" : "Ghosts"
    }
]

需要帮助来找到正确的函数,以将其转换为如下所示的简单列表-

joinResults: ["Spouses", "Children", "Ghosts"]

1 个答案:

答案 0 :(得分:2)

您只能在聚合管道中使用简单的$addFields

db.collection.aggregate([
  {
    $addFields: {
      joinResults: "$joinResults.value"
    }
  }
])

会放弃;

{
    //some id,
    "joinResults": [
        "Spouses",
        "Children",
        "Ghosts"
     ]
}

See it on mongoplayground