Mongodb-基于特定属性合并嵌套子文档数组

Mongodb-基于特定属性合并嵌套子文档数组,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我构建了一个聚合,返回以下数据: [ { "users": [ { _id: 1, name: "John", age: 31 }, { _id: 2, name: "Jane", age: 26

我构建了一个聚合,返回以下数据:

 [
      {
        "users": [
          {
            _id: 1,
            name: "John",
            age: 31
          },
          {
            _id: 2,
            name: "Jane",
            age: 26
          }
        ],
        "teams": [
          {
            id: 1,
            name: "Team 1",
            color: "yellow"
          },
          {
            id: 2,
            name: "Team 2",
            color: "red"
          }
        ],
        "moreTeams": [
          {
            id: 1,
            name: "Team 1 - More",
          },
          {
            id: 2,
            name: "Team 2 - More",
          },
          {
            id: 3,
            name: "Team 3 - More",
            extra: "extra"
          }
        ]
      }
    ]
如何将“teams”和
moreTeams
分组到同一个数组中(基于id),保留所有属性并最终覆盖它们具有相同名称的位置

以下是我想要的结果:

 [
          {
            "users": [
              {
                _id: 1,
                name: "John",
                age: 31
              },
              {
                _id: 2,
                name: "Jane",
                age: 26
              }
            ],
            "groupedTeams": [
              {
                id: 1,
                name: "Team 1 - More",
                color: "yellow"
              },
              {
                id: 2,
                name: "Team 2 - More",
                color: "red"
              },
              {
                id: 3,
                name: "Team 3 - More",
                extra: "extra"
              }
            ]
          }
        ]
我尝试过使用$unwind和$group,但没有成功:(

Palyground示例:

  • $map
    迭代
    moreTeams
    数组的循环
  • $filter
    迭代
    团队的循环
    数组,并通过
    id
  • $arrayElemAt
    从上述过滤器获取第一个元素对象
  • $mergeObjects
    将上面的对象从
    arrayElemAt
    和当前对象合并
现在我们在
teams
中合并了
moreTeams
,但我们也必须在
moreTeams
中合并
teams

  • $project
    $filter
    迭代
    团队的循环
    数组,如果
    id
    不在
    moreTeams
  • $concatarray
    到concat
    moreTeams
    及以上不在
    moreTeams

db.collection.aggregate([
  {
    $addFields: {
      moreTeams: {
        $map: {
          input: "$moreTeams",
          as: "m",
          in: {
            $mergeObjects: [
              {
                $arrayElemAt: [
                  {
                    $filter: {
                      input: "$teams",
                      cond: { $eq: ["$$m.id", "$$this.id"] }
                    }
                  },
                  0
                ]
              },
              "$$m"
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      users: 1,
      moreTeams: {
        $concatArrays: [
          "$moreTeams",
          {
            $filter: {
              input: "$teams",
              cond: { $not: { $in: ["$$this.id", "$moreTeams.id"] } }
            }
          }
        ]
      }
    }
  }
])