Vladimir Loct
Vladimir Loct

Reputation: 35

Aggregate the arrays in MongoDB (2)

I have a database structured like this:

 {
                "teams" : [
                    {
                        "best_players" : [
                            {
                                "contact" : {
                                    "name" : "SomeName1"
                                }, 
                                "characteristic" : {
                                    "skills" : "good"
                                 }
                                },
                                {
                                "contact" : {
                                    "name" : "SomeName2"
                                }, 
                                "characteristic" : {
                                    "skills" : "good"
                                 }
                                }

                        ], 
                        "teamname" : "SomeTeam1"
                    }, 
                    {
                    "best_players" : [
                        {
                            "contact" : {
                                "name" : "SomeName3"
                            }, 
                            "characteristic" : {
                                "skills" : "bad"
                             }
                            }
                    ], 
                    "teamname" : "SomeTeam2"
                } 
                    ]
              }

I need to rename arrays and fields, and see the information in a different form. What i'm expecting with aggregation-framework:

    {
    "team_players" : [
    {
    "player_name" : "SomeName1",
    "player_skills" : "good" ,
    "team_name" : "SomeTeam1"
    },
    {
    "player_name" : "SomeName2",
    "player_skills" : "good" ,
    "team_name" : "SomeTeam1"
    },
    {
    "player_name" : "SomeName3",
    "player_skills" : "bad" ,
    "team_name" : "SomeTeam2"
    }
    ]
    }

What is the right way to query my result with aggregation-framework?

Upvotes: 0

Views: 38

Answers (2)

Senthur Deva
Senthur Deva

Reputation: 787

This is working fine

     db.are.aggregate([
    { "$unwind": "$teams" },
    { "$unwind": "$teams.best_players" },
    {
        "$group": {
            "_id": null, "team_players": {
                "$push":
                {
                    "player_name": "$teams.best_players.contact.name",
                    "player_skills": "$teams.best_players.characteristic.skills",
                    "team_name": "$teams.teamname"
                }
            }
        }
    }
])

Upvotes: 1

s7vr
s7vr

Reputation: 75984

You can use $reduce with $concatArrays to transform all the best_players.

Something like

db.colname.aggregate(
{"$project":{
  "team_players":{
    "$reduce":{
      "input":"$teams",
      "initialValue":[],
      "in":{"$concatArrays":[
        "$$value",
         {"$map":{
            "input":"$$this.best_players",
            "as":"bp",
            "in":{
              "player_name":"$$bp.contact.name",                             
              "player_skills":"$$bp.characteristic.skills",
              "team_name":"$$this.teamname"
           }
        }}
      ]}
    }
  }
}});

Upvotes: 0

Related Questions