使用$ lookup和$ group左连接

时间:2016-05-03 09:30:18

标签: mongodb aggregation-framework lookup

我有2个集ServiceProviderParents

的ServiceProvider:

{
    "_id" : ObjectId("57285bc0588aeca4178b4567"),
    "ServiceProviderID" : 193,
    "Title" : "test1",
    "PostedMessage" : "test1",
    "TotalComments" : 0,
    "TotalShares" : 0,
    "TotalThanks" : 0,
    "AddedOn" : "2016-05-03 08:05:20",
    "LastModifiedOn" : "2016-05-03 08:05:20",
    "PostAttachment" : {
        "ImagePath" : ""
    },
    "PostCommentUserIDs" : [
        "193",
        "3"
    ],
    "PostComments" : [
        {
            "ID" : "14622628001303558386",
            "UserID" : 3,
            "CommentMessage" : "comment by test",
            "TotalThanks" : 0,
            "AddedOn" : "2016-05-03 08:06:40",
            "LastModifiedOn" : "2016-05-03 08:06:40",
            "CommentAttachment" : {
                "ImagePath" : ""
            }
        }
    ],
    "Subscriber" : [
        "193",
        "3"
    ]
}

父母:

{
    "_id" : ObjectId("572447e6588aec32108b4567"),
    "ID" : "1",
    "UID" : "2uwWMABSjBPWdAlSnglAlsY7A1cD1Q7EmHiM9wehxXyY99AQs6eC7rEt",
    "Name" : "test1",
    "ProfileImagePath" : "Penguins_1#SIZE#.jpg"
}
{
    "_id" : ObjectId("572447e6588aec32108b4568"),
    "ID" : "2",
    "UID" : "NM8H6oYQW33dRPxn48wkcMYyciYqagRiXUO0cLh0Lvws6eC7rEt",
    "Name" : "test2",
    "ProfileImagePath" : "fb_profilepic_879157302148180#SIZE#.jpg"
}
{
    "_id" : ObjectId("572447e6588aec32108b4569"),
    "ID" : "3",
    "UID" : "0Xb1fHqzR3HZVJWQc8aAbB77UkwSnmasdQrtp9qySVdqipsYs6eC7rEt",
    "Name" : "test3",
    "ProfileImagePath" : "images3_3_1460348144#SIZE#.jpeg"
}
{
    "_id" : ObjectId("572447e6588aec32108b456a"),
    "ID" : "4",
    "UID" : "xXyY99NaNixXyY99IfuJx4Is8Fv15t9lMC8RYpSgaAbB77zmB65c7vccvYs6eC7rEt",
    "Name" : "test4",
    "ProfileImagePath" : "Jyoti_4#SIZE#.png"
}
{
    "_id" : ObjectId("572447e6588aec32108b456b"),
    "ID" : "5",
    "UID" : "leyerH3hMY8UA3ONddJHDOHxXyY99IhhOgAImLZoQJ10RRxXyY99Us6eC7rEt",
    "Name" : "test8",
    "ProfileImagePath" : "woman-1_5#SIZE#.jpg"
}
{
    "_id" : ObjectId("572447e6588aec32108b456c"),
    "ID" : "6",
    "UID" : "B414HAxXyY996QezhAmWgJQmjoWUEhsNghXAcKRTEbNNHJXYs6eC7rEt",
    "Name" : "test5",
    "ProfileImagePath" : "Chrysanthemum_6#SIZE#.jpg"
}

我希望ServiceProvider.PostCommentUserIDs加入Parents集合以获取父信息。

我尝试过以下查询:

db.ServiceProvider.aggregate([
   {
      $unwind: "$PostCommentUserIDs"
   },
   {
      $lookup:
         {
            from: "Parents",
            localField: "PostCommentUserIDs",
            foreignField: "ID",
            as: "ParentDetailsArr"
        }
   },
   {
      $match: { "ParentDetailsArr": { $ne: [] }}
   }
])

但是它给了我 2个结果和相同的ServiceProvider._id相同的文档。

{
    "_id" : ObjectId("57285bc0588aeca4178b4567"),
    "ServiceProviderID" : 193,
    "Title" : "test1",
    "PostedMessage" : "test1",
    "TotalComments" : 0,
    "TotalShares" : 0,
    "TotalThanks" : 0,
    "AddedOn" : "2016-05-03 08:05:20",
    "LastModifiedOn" : "2016-05-03 08:05:20",
    "PostAttachment" : {
        "ImagePath" : ""
    },
    "PostCommentUserIDs" : "193",
    "PostComments" : [
        {
            "ID" : "14622628001303558386",
            "UserID" : 3,
            "CommentMessage" : "comment by test",
            "TotalThanks" : 0,
            "AddedOn" : "2016-05-03 08:06:40",
            "LastModifiedOn" : "2016-05-03 08:06:40",
            "CommentAttachment" : {
                "ImagePath" : ""
            }
        }
    ],
    "Subscriber" : [
        "193",
        "3"
    ],
    "ParentDetailsArr" : [
        {
            "_id" : ObjectId("572447e6588aec32108b45c6"),
            "ID" : "193",
            "UID" : "tvzRNsSiOVca8DoOEV9tMkzU4e0Rx4aMMDcQw3fb88Us6eC7rEt",
            "Name" : "test1",
            "ProfileImagePath" : null
        },
        {
            "_id" : ObjectId("572856a4588aec01178b45c6"),
            "ID" : "193",
            "UID" : "tvzRNsSiOVca8DoOEV9tMkzU4e0Rx4aMMDcQw3fb88Us6eC7rEt",
            "Name" : "test1",
            "ProfileImagePath" : "images_193_1462255293#SIZE#.jpg"
        }
    ]
}
{
    "_id" : ObjectId("57285bc0588aeca4178b4567"),
    "ServiceProviderID" : 193,
    "Title" : "test1",
    "PostedMessage" : "test1",
    "TotalComments" : 0,
    "TotalShares" : 0,
    "TotalThanks" : 0,
    "AddedOn" : "2016-05-03 08:05:20",
    "LastModifiedOn" : "2016-05-03 08:05:20",
    "PostAttachment" : {
        "ImagePath" : ""
    },
    "PostCommentUserIDs" : "3",
    "PostComments" : [
        {
            "ID" : "14622628001303558386",
            "UserID" : 3,
            "CommentMessage" : "comment by test",
            "TotalThanks" : 0,
            "AddedOn" : "2016-05-03 08:06:40",
            "LastModifiedOn" : "2016-05-03 08:06:40",
            "CommentAttachment" : {
                "ImagePath" : ""
            }
        }
    ],
    "Subscriber" : [
        "193",
        "3"
    ],
    "ParentDetailsArr" : [
        {
            "_id" : ObjectId("572447e6588aec32108b4569"),
            "ID" : "3",
            "UID" : "0Xb1fHqzR3HZVJWQc8aAbB77UkwSnmasdQrtp9qySVdqipsYs6eC7rEt",
            "Name" : "test2",
            "ProfileImagePath" : "images3_3_1460348144#SIZE#.jpeg"
        },
        {
            "_id" : ObjectId("572856a4588aec01178b4569"),
            "ID" : "3",
            "UID" : "0Xb1fHqzR3HZVJWQc8aAbB77UkwSnmasdQrtp9qySVdqipsYs6eC7rEt",
            "Name" : "test2",
            "ProfileImagePath" : "images3_3_1460348144#SIZE#.jpeg"
        }
    ]
}

我的问题是如何通过_id应用分组或如何重建上述查询,以便我获得每个文档的父信息数组。

1 个答案:

答案 0 :(得分:2)

这就是放松的方式。正如https://docs.mongodb.org/v3.0/reference/operator/aggregation/unwind/

所述

考虑包含以下文档的清单:

{ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] }

以下聚合使用$ unwind阶段为sizes数组中的每个元素输出文档:

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

该操作返回以下结果:

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }

标准情况是,您要展开要查找/排序的资源或使用它进行任何其他操作,然后将其分组以使其达到与以前相同的“相同状态”。

在您的情况下,您可能希望将其分组为

{$group : { _id : "$_id" } }

您很可能需要在之后使用$ push(google it)