我使用$ lookup来加入mongoDB中的不同集合。现在我面临一个问题,假设我有3个收藏品。
user_movies
{
"_id": "_id" : ObjectId("5834ecf7432d92675bde9d83"),
"mobile_no": "7941750156"
"movies" : ["dallas00", "titanic00", "green_mile00"]
}
影
{
"_id": "_id" : ObjectId("4834eff7412d9267556d9d52"),
"movie_name" : "Dallas Buyer's Club",
"movie_id": "dallas00",
"active": 0
}
movie_comments
{
"_id": "_id" : ObjectId("1264eff7412d92675567h576"),
"movie_id" : "dallas00",
"comment": "what a great movie.",
"time_posted": "1480516635131"
},
{
"_id": "_id" : ObjectId("1264eff7412d92675567h578"),
"movie_id" : "dallas00",
"comment": "awesome movie.",
"time_posted": "1480516635141"
},
{
"_id": "_id" : ObjectId("1264eff7412d92675567h567"),
"movie_id" : "titanic00",
"comment": "titanic awesome movie.",
"time_posted": "1480516635132"
},
{
"_id": "_id" : ObjectId("1264eff7412d92675567h579"),
"movie_id" : "green_mile00",
"comment": "Tom hanks did awesome movie.",
"time_posted": "1480516635133"
}
用户电影是我存储用户喜欢的电影的集合,电影是我存储有关电影的所有细节的集合,电影评论是我存储与电影相关的评论的集合。
现在我想写一个查询,我将向用户显示他们喜欢的电影列表,但所有电影必须有"活跃" :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和电影)上执行连接,但我无法在同一查询中对第三个集合执行查找。我想要的是发送第一次查找的输出数组 movie_ids ,并使用 movie_comments 将其发送到下一次查找。所以我可以将所有评论与我在movie_ids数组中的电影相关联。
现在有人可以告诉我为什么我没有得到任何输出,我可以使用查找,就像我第二次使用它(使用localfield作为$ group字段)或不是我怎么能执行此操作单个查询中的功能。
更新 - 现在只剩下一件事,我想以排序的方式获得结果,以便电影的排序方式与出现在" user_movies"数组(根据它们在数组中的位置)及其相应的注释将根据' 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."]
},
Ok最后一件事如果可以获得电影数组字符串的位置,当我执行第一次查找时匹配然后我可以根据我将给予的重量(更多权重到先前的位置等等)进行排序。所以现在我要做的是{{$ 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}, */
答案 0 :(得分:4)
好吧,看起来你有很多问题。我会试着总结一下。
问题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();