Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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 将文档与其嵌套数组及其嵌套数组合并_Mongodb_Aggregation Framework - Fatal编程技术网

Mongodb 将文档与其嵌套数组及其嵌套数组合并

Mongodb 将文档与其嵌套数组及其嵌套数组合并,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我正在尝试使用聚合框架创建一个查询,但无法获得所需的结果。 我有一个经销商集合,每个经销商有一个客户列表,每个客户有一个成员列表,结构如下: [ { "userID" : "xxx", "userType" : "RESELLER", "clients" : [ { "userID" : "xxx", "userType" : "CLIENT", "members" : [ {

我正在尝试使用聚合框架创建一个查询,但无法获得所需的结果。 我有一个经销商集合,每个经销商有一个客户列表,每个客户有一个成员列表,结构如下:

[
{
  "userID" : "xxx",
  "userType" : "RESELLER",
  "clients" : [
     {
        "userID" : "xxx",
        "userType" : "CLIENT",
        "members" : [
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           },
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           }
        ]
     },
{
        "userID" : "xxx",
        "userType" : "CLIENT",
        "members" : [
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           },
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           }
        ]
     }
   ]
},
{
  "userID" : "xxx",
  "userType" : "RESELLER",
  "clients" : [
     {
        "userID" : "xxx",
        "userType" : "CLIENT",
        "members" : [
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           },
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           }
        ]
     },
{
        "userID" : "xxx",
        "userType" : "CLIENT",
        "members" : [
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           },
           {
              "userID" : "xxx",
              "userType" : "MEMBER"
           }
        ]
     }
   ]
}
]
我想得到的结果是:

[
   {
      "userID" : "xxx",
      "userType" : "RESELLER"
   },
   {
      "userID" : "xxx",
      "userType" : "RESELLER"
   },
   {
      "userID" : "xxx",
      "userType" : "CLIENT"
   },
   {
      "userID" : "xxx",
      "userType" : "CLIENT"
   },
   {
      "userID" : "xxx",
      "userType" : "CLIENT"
   },
   {
      "userID" : "xxx",
      "userType" : "CLIENT"
   },
   {
      "userID" : "xxx",
      "userType" : "MEMBER"
   },
   {
      "userID" : "xxx",
      "userType" : "MEMBER"
   },
   {
      "userID" : "xxx",
      "userType" : "MEMBER"
   },
   {
      "userID" : "xxx",
      "userType" : "MEMBER"
   },
   {
      "userID" : "xxx",
      "userType" : "MEMBER"
   }
]
我做了很多尝试,但我没有得到这个结果。 我所做的最接近的解决方案是:

db.resellers.aggregate([
{
    $unwind: "$clients"
},
{
    $project: {
        _id : 0,
        teamMembers : "$clients.members"
    }
},
{
    $unwind: "$members"
},
{
    $project: {
        _id : 0,
        userID : "$members.userID",
        type : "$members.type"
    }
}
]).pretty()
此解决方案仅返回成员列表,因此我必须做些什么才能获得一个包含所有经销商、客户和成员的列表?

您可以使用来扁平化数据结构,然后运行以获得每个文档的单个成员:

db.collection.aggregate([
  { "$project": {
    "members": {
      "$concatArrays": [
        [{ "userID": "$userID", "userType": "$userType" }],
        { "$reduce": {
          "input": "$clients",
          "initialValue": [],
          "in": {
            "$concatArrays": [
              "$$value",
              [{ "userID": "$$this.userID", "userType": "$$this.userType" }],
              "$$this.members"
            ]
          }
        }}
      ]
    }
  }},
  { "$unwind": "$members" },
  { "$replaceRoot": { "newRoot": "$members" }}
])

好吧,您可以在
$project
阶段完成这项工作

[
    {
        "$project": {
            "members": {
                "$reduce": {
                    "input": {
                        "$map": {
                            "input": "$clients",
                            "in": {
                                "$concatArrays": [
                                    [
                                        {
                                            "userID": "$userID",
                                            "userType": "$userType"
                                        },
                                        {
                                            "userID": "$$this.userID",
                                            "userType": "$$this.userType"
                                        }
                                    ],
                                    "$$this.members"
                                ]
                            }
                        }
                    },
                    "initialValue": [

                    ],
                    "in": {
                        "$concatArrays": [
                            "$$this",
                            "$$value"
                        ]
                    }
                }
            }
        }
    }
]

谢谢styvane,您的解决方案有效,我只需使用replaceRoot函数将“$members”展开并使其成为新的根目录。使用此查询,您无需
$unwind
数组或使用
$replaceRoot