Home > Enterprise >  How to unwind Object of Array in Mongodb
How to unwind Object of Array in Mongodb

Time:10-11

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 unwind the array inside the Object and also want to merge both arrays into one.

Sample Document:

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

And the desired output is:

{
    team_a: [
      {
          username: "test_1",
          score: "33",
          rank: "1",
          prize: 15.4,
      },
      {
          username: "test_2",
          score: "31",
          rank: "2",
          prize: 15.4,
       },
       {
         username: "test_3",
         score: "30",
         rank: "3",
         prize: 15.4,
      }
    ],
    team_b: [
      {
         username: "test_4",
         score: "32",
         rank: "1",
      },
      {
        username: "test_5",
        score: "30",
        rank: "2",
       },
    ],
    all_winners: [
        {
          username: "test_1",
          score: "33",
          rank: "1",
          prize: 15.4,
        },
        {
          username: "test_2",
          score: "31",
          rank: "2",
          prize: 15.4,
        },
        {
          username: "test_3",
          score: "30",
          rank: "3",
          prize: 15.4,
        }
        {
          username: "test_4",
          score: "31",
          rank: "4",
        },
        {
          username: "test_5",
          score: "30",
          rank: "5",
        },
    ]
 }

Any guidance or pointers are gratefully received. Thanks

CodePudding user response:

Solution 1

1st $project stage:

  1. team_a field with team_a.players
  2. team_b field with team_b.players
  3. all_winners field with $concatArrays for team_a.players and team_b.players

2nd $project stage:

  • Remove id field from team_a, team_b, all_winners array fields.
db.collection.aggregate([
  {
    $project: {
      "team_a": "$team_a.players",
      "team_b": "$team_b.players",
      "all_winners": {
        "$concatArrays": [
          "$team_a.players",
          "$team_b.players"
        ]
      }
    }
  },
  {
    $project: {
      "all_winners": {
        id: 0
      },
      "team_a": {
        id: 0
      },
      "team_b": {
        id: 0
      }
    }
  }
])

Sample Mongo Playground (Solution 1)


Solution 2

Alternative, use $unset to remove team_a.players.id and team_b.players.id as first stage.

db.collection.aggregate([
  {
    $unset: [
      "team_a.players.id",
      "team_b.players.id"
    ]
  },
  {
    $project: {
      "team_a": "$team_a.players",
      "team_b": "$team_b.players",
      "all_winners": {
        "$concatArrays": [
          "$team_a.players",
          "$team_b.players"
        ]
      }
    }
  }
])

Sample Mongo Playground (Solution 2)

  • Related