集合A: id为"mid“的电影列表:
> db.moviesCollection.find().pretty()
{
"_id" : ObjectId("57266290482b0b6d8ad6a8bd"),
"mid" : 1,
"title" : "Toy Story (1995)",
"genres" : "Animation|Children's|Comedy"
}
{
"_id" : ObjectId("57266290482b0b6d8ad6a8be"),
"mid" : 3,
"title" : "Grumpier Old Men (1995)",
"genres" : "Comedy|Romance"
}收录B:"mid“分级电影列表
> db.ratingsCollection.find().pretty()
{
"_id" : ObjectId("57266359482b0b6d8ad6b7e8"),
"uid" : 1,
"mid" : 1193,
"rating" : 5,
"timestamp" : 978300760
}
{
"_id" : ObjectId("57266359482b0b6d8ad6b7e9"),
"uid" : 1,
"mid" : 661,
"rating" : 3,
"timestamp" : 978302109
}我的问题是:如何从(ratingsCollection)中存在(Mid)的(moviesCollection)中获取所有电影标题,即分级电影的标题?提前进行thx检查。
发布于 2016-05-09 06:10:02
db.r ->分级db.m ->电影
聚合框架附带了3.2版的$lookup --这是提议的解决方案的核心部分。
db.r.aggregate([{
$lookup : {
from : "m",
localField : "mid",
foreignField : "mid",
as : "result"
}
}, {
$match : {
"result" : {
$exists : true,
$ne: []
}
}
}
])欢迎任何彗星!
发布于 2016-05-09 06:10:36
您可以这样做:
db.moviesCollection.aggregate([
{
$match: {
"mid": {
$in: db.ratingsCollection.find().map(function(r) { return r.mid; })
}
}
}
])发布于 2016-05-09 05:48:44
select * from A where exists (select mid from B where B.mid = A.mid)https://stackoverflow.com/questions/37105151
复制相似问题