如何在mongoDB中将2个字段合并到数组中

时间:2017-02-21 12:08:28

标签: mongodb

我需要将多个字段折叠成一个数组,我有一个Talend作业的文档,其文档结构与下面类似。

你可以看到有多个"部门" "位置内的文件"文档,以及 - 如果存在部门记录(1或更多),我需要将它们填入数组中。

{
_id: "1010101010101010",
COUNTRY_ID: "1",
COUNTRY_NAME: "US",
location: [
    {
       location_id: "99",
       location_name: "Washington",
       department: 
          {
             department_id: "100",
             department_name: "Finance"
          },
      department: 
          { 
             department_id: "101",
             department_name: "Sales"
          }
     }
  ]
}

我需要它成为:

   {
_id: "1010101010101010",
COUNTRY_ID: "1",
COUNTRY_NAME: "US",
location: [
    {
       location_id: "99",
       location_name: "Washington",
       department: 
          [ 
            {
               department_id: "100",
               department_name: "Finance"
            },
            { 
               department_id: "101",
               department_name: "Sales"
            }
         ]
     }
  ]
}

看起来我想要一个$ unwind动作的反面但不熟悉mongoDB。任何帮助表示赞赏。

0 个答案:

没有答案