如何加入深层嵌套数组?

时间:2018-02-01 08:23:28

标签: mongodb mongoose aggregation-framework

这是我的实际数据库架构。 company_id是公司集合的参考对象,booking_days.consultants.consultant_id是用户集合的参考对象。

我想加入嵌入式文档与company_id和booking_days.consultants.consultant_id。

{
    "_id" : ObjectId("5a7040d664544e1bb877deae"),
    "company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
    "booking_days" : [ 
        {
            "booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877deca"),
            "consultants" : [ 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
                    "_id" : ObjectId("5a7040d664544e1bb877decc")
                }, 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
                    "_id" : ObjectId("5a7040d664544e1bb877decb")
                }
            ]
        }, 
        {
            "booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877dec6"),
            "consultants" : [ 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
                    "_id" : ObjectId("5a7040d664544e1bb877dec9")
                }, 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
                    "_id" : ObjectId("5a7040d664544e1bb877dec8")
                }, 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
                    "_id" : ObjectId("5a7040d664544e1bb877dec7")
                }
            ]
        }, 
        {
            "booking_date" : ISODate("2018-02-02T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877dec4"),
            "consultants" : [ 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
                    "_id" : ObjectId("5a7040d664544e1bb877dec5")
                }
            ]
        }, 

    ],
    "__v" : 0
}

我正在使用以下查询。

db.getCollection('booking_days').aggregate(
  [
     { $match: { company_id:ObjectId("5a6eb43f437e6a0d9e00c92f") } },
     { 
         $lookup: { 
                    localField: "company_id",
                    from: "companies",
                    foreignField: "_id",
                    as: "companies"
                 },

     },
      { 
         $lookup: { 
                    localField: "booking_days.consultants.consultant_id",
                    from: "users",
                    foreignField: "_id",
                    as: "userssss"
                 },

     },
      { 
        $unwind:"$companies"
      },


   ]
  )
  

实际输出

{
    "_id" : ObjectId("5a7040d664544e1bb877deae"),
    "company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
    "booking_days" : [ 
        {
            "booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877deca"),
            "consultants" : [ 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
                    "_id" : ObjectId("5a7040d664544e1bb877decc")
                }, 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
                    "_id" : ObjectId("5a7040d664544e1bb877decb")
                }
            ]
        }, 
        {
            "booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877dec6"),
            "consultants" : [ 
                {
                    "consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
                    "_id" : ObjectId("5a7040d664544e1bb877dec9")
                }, 

            ]
        }, 
    ],
    "__v" : 0,
    "companies" : {
        "_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
        "first_name" : "Adrienne Runolfsson",
    },
    "users" : [ 
        {
            "_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
            "first_name" : "Christ Hamill",

        }, 
        {
            "_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
            "first_name" : "Miss Dina Kovacek",

        }, 

    ]
}
  

例外输出。顾问数据将出现在booking_days.consultants数组中。

{
    "_id" : ObjectId("5a7040d664544e1bb877deae"),
    "company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
    "booking_days" : [ 
        {
            "booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877deca"),
            "consultants" : [ 
                {
                    "consultant_id" : {
                        "_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
                        "first_name" : "Christ Hamill",
                    },
                    "_id" : ObjectId("5a7040d664544e1bb877decc")
                }, 
                {
                    "consultant_id" :  {
                        "_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
                        "first_name" : "Miss Dina Kovacek",
                    },
                    "_id" : ObjectId("5a7040d664544e1bb877decb")
                }
            ]
        }, 
        {
            "booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
            "_id" : ObjectId("5a7040d664544e1bb877dec6"),
            "consultants" : [ 
                {
                    "consultant_id" :  {
                        "_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
                        "first_name" : "Miss Dina Kovacek",
                    }, 
                    "_id" : ObjectId("5a7040d664544e1bb877dec9")
                }, 

            ]
        }, 
    ],
    "__v" : 0,
    "companies" : {
        "_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
        "first_name" : "Adrienne Runolfsson",
    },

}

2 个答案:

答案 0 :(得分:0)

因此,当&avoid=tolls是嵌入式文档数组时,您必须$unwind localField期望localField是一个标量ID数组。

$unwind两次作为顾问数组是两级深度,然后$lookup获取名称,$group获取预期输出。

db.getCollection('booking_days').aggregate([
  {"$match":{"company_id":ObjectId("5a6eb43f437e6a0d9e00c92f")}},
  {"$lookup":{"localField":"company_id","from":"companies","foreignField":"_id","as":"companies"}},
  {"$unwind":"$companies"},
  {"$unwind":"$booking_days"},
  {"$unwind":"$consultants"},
  {"$lookup":{
    "localField":"booking_days.consultants.consultant_id",
    "from":"users",
    "foreignField":"_id",
    "as":"booking_days.consultants.consultant_id"
  }},
  {"$group":{
    "_id":{"_id":"$_id","booking_days_id":"$booking_days._id"},
    "company_id":{"$first":"$company_id"},
    "booking_date":{"$first":"$booking_days.booking_date"},
    "companies":{"$first":"$companies"},
    "consultants":{"$push":"$booking_days.consultants"}
  }},
  {"$group":{
    "_id":"$_id._id",
    "company_id":{"$first":"$company_id"},
    "companies":{"$first":"$companies"},
    "booking_days":{
      "$push":{
        "_id":"$_id.booking_days_id",
        "booking_date":"$booking_date",
        "consultants":"$consultants"
      }
    }
  }}
])

答案 1 :(得分:0)

regex = re.compile(r"*(feedback)*(report)*.xlsx", flags = re.IGNORECASE)

accountId连接不同

{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [ 
        {
            "likes" : [],
            "_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
            "comment" : "string",
            "accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
            "commentId" : "7d2a05d1-2026-4a13-a5c1-318ed80d1b38",
            "reply" : [ 
                {
                    "_id" : ObjectId("5b87b61e97585ef1d0d22108"),
                    "comment" : "string",
                    "accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
                    "replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
                },
                {
                    "_id" : ObjectId("5b87b61e97585ef1d0d22108"),
                    "comment" : "string klllll",
                    "accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
                    "replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
                }
            ]
        }, 
        {
            "likes" : [],
            "_id" : ObjectId("5b87c301c8a07efa2599c29e"),
            "comment" : "testing",
            "accountId" : "cfd29f53-d73e-480c-9cfa-ea42b4119266",
            "commentId" : "0676047b-1712-4f70-89d5-29c1abe03eaf",
            "reply" : [
                      {
                    "_id" : ObjectId("5b87b61e97585ef1d0d22108"),
                    "comment" : "string",
                    "accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
                    "replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
                },
                {
                    "_id" : ObjectId("5b87b61e97585ef1d0d22108"),
                    "comment" : "string klllll",
                    "accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
                    "replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
                }
            ]
        }
    ]
}