左加入 $lookup 和 $group

Left join with $lookup and $group

我有 2 个 collection ServiceProviderParents

服务提供商:

{
    "_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"
    ]
}

Parents:

{
    "_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.PostCommentUserIDsParents collection 以获取 parent 信息。

我试过以下查询:

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 应用分组或如何重建上述查询以便我获得每个文档的 Parent 信息数组。

这就是 unwind 的作用。如 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" } ] )

运行returns结果如下:

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

标准情况是,您展开要 lookup/sort 的资源或对其进行任何其他操作,然后像以前一样对其进行分组以到达 "same state"。

对于您的情况,您可能希望将其分组如下

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

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