Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
聚合MongoDB中的数组(2)_Mongodb_Nosql_Mongodb Query_Aggregation Framework - Fatal编程技术网

聚合MongoDB中的数组(2)

聚合MongoDB中的数组(2),mongodb,nosql,mongodb-query,aggregation-framework,Mongodb,Nosql,Mongodb Query,Aggregation Framework,我的数据库结构如下: { "teams" : [ { "best_players" : [ { "contact" : { "name" : "SomeName1"

我的数据库结构如下:

 {
                "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"
                } 
                    ]
              }
我需要重命名数组和字段,并以不同的形式查看信息。我对聚合框架的期望是:

    {
    "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"
    }
    ]
    }
使用聚合框架查询我的结果的正确方法是什么?

您可以使用来转换所有最佳玩家

差不多

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"
           }
        }}
      ]}
    }
  }
}});
这很好用

     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"
                }
            }
        }
    }
])