How to merge to Objects of Array

I am trying to manipulate a dataset to make it easy to display in mongoCharts. There are two sets of team_a and team_b, Each contains a set of playerId, score, rank and prize. I want to merge both arrays into one.

Sample Document:

{
  _id: "6138669dde7a271290906e25",
  team_a: {
    team_score: 64,
    players: [
      {
        id: "604f00d43776e45a448628f9",
        score: "33",
        rank: "1",
        prize: 15.4,
      },
      {
        id: "60058dd9b88cc1a1e40f2f54",
        score: "31",
        rank: "2",
        prize: 15.4,
      },
    ],
    team_name: "team_1",
  },
  team_b: {
    team_score: 62,
    players: [
      {
        id: "602ce34a39c7496600940774",
        score: "32",
        rank: "1",
      },
      {
        id: "60058db6b88cc1a1e40f2f4f",
        score: "30",
        rank: "2",
      },
    ],
    team_name: "team_2",
  },
  prize_pool: 30.8,
  cut: 4,
  tax: 5.2,
  live_players: "4",
  completed: true,
  name: "match",
  entry_money: "10",
  expiration_time: "9/8/2021 12:33:06",
}

And desired output is:

all_winners: [
    {
      id: "604f00d43776e45a448628f9",
      score: "33",
      rank: "1",
    },
    {
      id: "602ce34a39c7496600940774",
      score: "32",
      rank: "2",
    },
    {
      id: "60058dd9b88cc1a1e40f2f54",
      score: "31",
      rank: "3",
    },
    {
      id: "60058db6b88cc1a1e40f2f4f",
      score: "30",
      rank: "4",
    },
  ],

Any guidance or pointers are gratefully received.
Thanks

Hi @Suneel_Kumar_Khatri and welcome in the MongoDB Community :muscle: !

Here you go:

db.coll.insert({ "_id": "6138669dde7a271290906e25", "team_a": { "team_score": 64, "players": [ { "id": "604f00d43776e45a448628f9", "score": "33", "rank": "1", "prize": 15.4 }, { "id": "60058dd9b88cc1a1e40f2f54", "score": "31", "rank": "2", "prize": 15.4 }], "team_name": "team_1" }, "team_b": { "team_score": 62, "players": [ { "id": "602ce34a39c7496600940774", "score": "32", "rank": "1" }, { "id": "60058db6b88cc1a1e40f2f4f", "score": "30", "rank": "2" }], "team_name": "team_2" }, "prize_pool": 30.8, "cut": 4, "tax": 5.2, "live_players": "4", "completed": true, "name": "match", "entry_money": "10", "expiration_time": "9/8/2021 12:33:06" })

Here is the aggregation pipeline which is just using a $project stage and a $concatArrays operator:

[
  {
    '$project': {
      '_id': 0, 
      'all_winners': {
        '$concatArrays': [
          '$team_a.players', '$team_b.players'
        ]
      }
    }
  }
]

Result:

[
  {
    all_winners: [
      {
        id: '604f00d43776e45a448628f9',
        score: '33',
        rank: '1',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f54',
        score: '31',
        rank: '2',
        prize: 15.4
      },
      { id: '602ce34a39c7496600940774', score: '32', rank: '1' },
      { id: '60058db6b88cc1a1e40f2f4f', score: '30', rank: '2' }
    ]
  }
]

This knowledge about the aggregation pipeline is covered in the M121 training on MongoDB University if you want to learn more.

Cheers,
Maxime.

1 Like

Hi Maxime, it worked perfectly thanks Man

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.