使用三个集合在 mongodb 中执行联接?

Perform joins in mongodb with three collections?

我正在使用 $lookup 连接 mongoDB 中的不同集合。现在我在这里遇到一个问题假设我有下面给出的 3 个集合。

user_movies

{
    "_id": ObjectId("5834ecf7432d92675bde9d83"),
    "mobile_no": "7941750156"
    "movies" : ["dallas00", "titanic00", "green_mile00"]
}

电影

{
    "_id" : ObjectId("4834eff7412d9267556d9d52"),
    "movie_name" : "Dallas Buyer's Club",
    "movie_id": "dallas00",
    "active": 0

}

movie_comments

{
    "_id": ObjectId("1264eff7412d92675567h576"),
    "movie_id" : "dallas00",
    "comment": "what a great movie.",
    "time_posted": "1480516635131"
},
{
    "_id": ObjectId("1264eff7412d92675567h578"),
    "movie_id" : "dallas00",
    "comment": "awesome movie.",
    "time_posted": "1480516635141"
},
{
    "_id": ObjectId("1264eff7412d92675567h567"),
    "movie_id" : "titanic00",
    "comment": "titanic awesome movie.",
    "time_posted": "1480516635132"
},
{
    "_id": ObjectId("1264eff7412d92675567h579"),
    "movie_id" : "green_mile00",
    "comment": "Tom hanks did awesome movie.",
    "time_posted": "1480516635133"
}

User Movies 是我存储用户喜欢的电影的集合,Movies 是我存储有关电影的所有详细信息的集合,movie_comments 是我存储与电影相关的评论的集合。

现在我想编写一个查询,向用户显示他们喜欢的电影列表,但所有电影都必须有 "active" : 1,以及与该特定电影相关的评论。现在我尝试使用 $lookup 我的代码如下。

db.user_movies.aggregate(
        {$match : {mobile_no : mobile_no}},
        { "$unwind": "$movies" },
        {$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
        {$unwind : "$bmarks"},
        {$match : {"bmarks.active": 1}},
        { $group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
        {$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}},
        {$unwind : "$comments"},
        {$sort: {time_posted: -1}},
        {$group: {_id: '$_id', comments : {$push : "$comments"}}},

我正在尝试编写一个查询来执行所有这些功能,并且我能够对前两个集合执行联接,即 user_movies 和电影,但我无法对第三个集合执行查找相同的查询。我想要的是发送我的 first lookup 的输出数组,即 movie_ids 并将其发送到 [=49= 的下一次查找]movie_comments。这样我就可以在 movie_ids 数组中获得与电影相关的所有评论。

现在谁能告诉我为什么我没有得到任何输出,我可以像第二次使用查找一样使用查找吗(使用 localfield 作为 $group 字段)或者我如何在一个查询中执行此功能.

更新- 现在只剩下一件事了,我想以一种排序的方式获得结果,这样电影就会像它们出现在“user_movies " array(根据他们在array中的位置)和他们对应的注释将按照'timeposted'降序排列。

所以对于上述文件,我希望我的输出像

{
     "movie_id": "dallas00"  // dallas00 is first because it appear first in "user_movies" movies array.
     "movie_name": "Dallas Buyer's Club",
     "comments": ["what a great movie.","awesome movie."]
},
{
     "movie_id": "titanic00"  // titanic00 is second because it appear second in "user_movies" movies array.
     "movie_name": "Titanic",
     "comments": ["titanic awesome movie."]
},
{
     "movie_id": "green_mile00"  // green_mile00 is third because it appear third in "user_movies" movies array.
     "movie_name": "Green mile",
     "comments": ["Tom hanks did awesome movie."]
},

好的最后一件事,如果可以获得电影数组字符串的位置,当我执行第一次查找时,我可以根据我将给予的权重进行排序(更多权重到前一个位置等等)。所以现在我要做的是{{$sort: { 'movie_weight': -1, 'time_posted': -1}}。现在我将对我的结果进行排序,这样电影将根据它们在数组 "user_movies" 数组中的位置进行排序,评论将根据与他们的电影相对应的 "time_posted" 进行排序。

更新代码

{$match : {mobile_no : mobile_no}},
        {$unwind: { path: "$movies", includeArrayIndex: "movieposition"}},
        {$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
        {$unwind : "$bmarks"},
        {$match : {"bmarks.active": 1}},
        {$group : { "_id" : "$bmarks.movie_id", movie_names : {$push : "$bmarks.movie_name"}, movie_ids: {$push : "$bmarks.movie_id"}, movie_position: {$push : "$movieposition"}}},
        {$unwind : "$movie_ids"},
        {$unwind : "$movie_names"},
        {$lookup: {from: "movie_comments", localField: "movie_ids", foreignField: "movie_id", as: "comments"}},
        {$unwind : "$comments"},
        {$sort: { 'movie_position': -1, 'comments.time_posted': -1 }},
        {$group: {_id: { movie_id: '$comments.movie_id', movie_name: '$movie_names' }, cmnts_ids: {$push: '$comments._id'}}},
        {$project: {_id: 0, movie_id: '$_id.movie_id', movie_name: '$_id.movie_name', tot_cmnts: {"$size" : "$cmnts_ids"}, top_cmnts_ids: {$slice: ['$cmnts_ids', 0, 4]}}}},
        {$skip: page*page_size},
        {$limit: page_size}, */

好的,看起来你有很多问题。我会试着总结一下。

问题 1:您提供的聚合代码无法编译。

db.user_movies.aggregate(
{$match : {mobile_no : mobile_no}},
{$unwind: "$movies" },
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind : "$bmarks"},
{$match : {"bmarks.active": 1}},
{$group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
{$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$sort: {time_posted: -1}},
{$group: {_id: '$_id', comments : {$push : "$comments"}}},

修复:

db.user_movies.aggregate([
{$match : {mobile_no : mobile_no}},
{$unwind: "$movies" },
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind : "$bmarks"},
{$match : {"bmarks.active": 1}},
{$group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
{$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$sort: {time_posted: -1}},
{$group: {_id: '$_id', comments : {$push : "$comments"}}}])

现在假设您使用的是您提供的相同数据。

问题 2:{$match : {"bmarks.active": 1}} 不匹配任何条目。

修复:{$match : {"bmarks.active": 0}}

问题 3:未定义查找字段 {$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}}

修复:{$lookup: {from: "movie_comments", localField: "movie_ids",foreignField: "movie_id",as: "comments"}}

问题 4:movie_ids 之前的查找没有展开阶段

修复:{$unwind : "$movie_ids"}

问题 5:没有发布时间字段 {$sort: {time_posted: -1}}

修复:在排序前包含字段

因此,要将所有内容放在一起,您需要汇总成如下所示的内容,以提取每部电影的评论。

db.user_movies.aggregate([
{$match : {mobile_no : mobile_no}},
{$unwind: "$movies"},
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind : "$bmarks"},
{$match : {"bmarks.active": 0}},
{$group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
{$unwind : "$movie_ids"},
{$lookup: {from: "movie_comments", localField: "movie_ids",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$group: {_id: '$_id', comments : {$push : "$comments"}}}])

示例输出

{
    "_id": ObjectId("5834ecf7432d92675bde9d83"),
    "comments": [{
        "_id": ObjectId("583d96d7e35f6e9c53c9e894"),
        "movie_id": "dallas00",
        "comment": "what a great movie."
    }, {
        "_id": ObjectId("583d96d7e35f6e9c53c9e895"),
        "movie_id": "dallas00",
        "comment": "awesome movie."
    }]
}

更新:

db.user_movies.aggregate([
{$match : {mobile_no : mobile_no}},
{$unwind: {path: "$movies", includeArrayIndex: "moviePosition"}},
{$sort :  {moviePosition:1}},
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind :"$bmarks"},
{$group : {_id : "$_id", movies : {$push : {movie_name:"$bmarks.movie_name", movie_id:"$bmarks.movie_id"} }}},
{$unwind : "$movies"},
{$lookup: {from: "movie_comments", localField: "movies.movie_id",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$group:  {_id: "$movies.movie_id", movie_name: {$first:"$movies.movie_name"}, comments : {$push : {comment:"$comments.comment", time_posted:"$comments.time_posted"}}}},
{$sort :  {time_posted:-1}},
{$project:{_id:0, movie_id:"$_id", movie_name:1, comments: "$comments.comment"}}
]).pretty();