How to merge Objects of Arrays

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:

{
  team_a: {
    team_score: 94,
    team_name: "team_1",
    players: [
      {
        id: "604f00d43776e45a448628f9",
        score: "33",
        rank: "1",
        prize: 15.4,
      },
      {
        id: "60058dd9b88cc1a1e40f2f54",
        score: "31",
        rank: "2",
        prize: 15.4,
      },
      {
        id: "60058dd9b88cc1a1e40f2f55",
        score: "30",
        rank: "3",
        prize: 15.4,
      }
    ],
  },
  team_b: {
    team_score: 62,
    team_name: "team_2",
    players: [
      {
        id: "602ce34a39c7496600940774",
        score: "32",
        rank: "1",
      },
      {
        id: "60058db6b88cc1a1e40f2f4f",
        score: "30",
        rank: "2",
      },
    ],
  },
}

And the desired output is:

{
    team_a: [
      {
          id: "604f00d43776e45a448628f9",
          score: "33",
          rank: "1",
          prize: 15.4,
      },
      {
          id: "60058dd9b88cc1a1e40f2f54",
          score: "31",
          rank: "2",
          prize: 15.4,
       },
       {
         id: “60058dd9b88cc1a1e40f2f55”, 
         score: "30",
         rank: "3",
         prize: 15.4,
      }
    ],
    team_b: [
      {
         id: "602ce34a39c7496600940774",
         score: "32",
         rank: "1",
      },
      {
        id: "60058db6b88cc1a1e40f2f4f",
        score: "30",
        rank: "2",
       },
    ],
    all_winners: [
        {
          id: "604f00d43776e45a448628f9",
          score: "33",
          rank: "1",
          prize: 15.4,
        },
        {
          id: "60058dd9b88cc1a1e40f2f54",
          score: "31",
          rank: "2",
          prize: 15.4,
        },
        {
          id: "60058dd9b88cc1a1e40f2f55", 
          score: "30",
          rank: "3",
          prize: 15.4,
        }
        {
          id: "60058dd9b88cc1a1e40f2f54",
          score: "31",
          rank: "4",
        },
        {
          id: "60058db6b88cc1a1e40f2f4f",
          score: "30",
          rank: "5",
        },
    ]
 }

Any guidance or pointers are gratefully received.
Thanks

Hi @Suneel_Kumar_Khatri,

Can you please confirm that there is a typo in your desired output because if I understood correctly what you are trying to do: all_winners = Team A + Team B.

Here I see that 60058dd9b88cc1a1e40f2f54 appears twice in the all_winners and the scores and ranks of the team B don’t match.

Anyway, I’ll assume what you want is exactly winners = A+B in my answer.

Here is the Aggregation Pipeline I’m using:

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

Here is the result in Mongosh.

test [direct: primary] test> db.coll.findOne()
{
  _id: ObjectId("61643cf2685087b27909f413"),
  team_a: {
    team_score: 94,
    team_name: 'team_1',
    players: [
      {
        id: '604f00d43776e45a448628f9',
        score: '33',
        rank: '1',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f54',
        score: '31',
        rank: '2',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f55',
        score: '30',
        rank: '3',
        prize: 15.4
      }
    ]
  },
  team_b: {
    team_score: 62,
    team_name: 'team_2',
    players: [
      { id: '602ce34a39c7496600940774', score: '32', rank: '1' },
      { id: '60058db6b88cc1a1e40f2f4f', score: '30', rank: '2' }
    ]
  }
}
test [direct: primary] test> db.coll.aggregate([
...   {
.....     '$project': {
.......       '_id': 0, 
.......       'team_a': '$team_a.players', 
.......       'team_b': '$team_b.players', 
.......       'all_winners': {
.........         '$concatArrays': [
.........           '$team_a.players', '$team_b.players'
.........         ]
.........       }
.......     }
.....   }
... ])
[
  {
    team_a: [
      {
        id: '604f00d43776e45a448628f9',
        score: '33',
        rank: '1',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f54',
        score: '31',
        rank: '2',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f55',
        score: '30',
        rank: '3',
        prize: 15.4
      }
    ],
    team_b: [
      { id: '602ce34a39c7496600940774', score: '32', rank: '1' },
      { id: '60058db6b88cc1a1e40f2f4f', score: '30', rank: '2' }
    ],
    all_winners: [
      {
        id: '604f00d43776e45a448628f9',
        score: '33',
        rank: '1',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f54',
        score: '31',
        rank: '2',
        prize: 15.4
      },
      {
        id: '60058dd9b88cc1a1e40f2f55',
        score: '30',
        rank: '3',
        prize: 15.4
      },
      { id: '602ce34a39c7496600940774', score: '32', rank: '1' },
      { id: '60058db6b88cc1a1e40f2f4f', score: '30', rank: '2' }
    ]
  }
]

Cheers,
Maxime.