我试图用聚合框架创建一个查询,但无法得到我想要的结果。
我有一个转销商的集合,每个转销商有一个客户列表,每个客户有一个成员列表,结构如下:

[
{
  "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()

这个解决方案只返回成员列表,所以我要做什么才能得到一个包含所有转销商、客户和成员的列表?

最佳答案

您可以使用$reduce$concatArrays来展开数据结构,然后使用$unwind$replaceRoot运行Mongo Playground来获取每个文档的单个成员:

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

关于mongodb - 将文档与其嵌套数组及其嵌套数组合并,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57101367/

10-09 17:46