MongoDB:查找集合B中存在的集合A中的数据

时间:2016-05-08 21:45:22

标签: mongodb mongodb-query

收藏A:以“mid”作为id:

的电影列表
> 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
}

我的问题:我如何从(moviesCollection)获得所有电影片头,(ratingsCollection),评级电影的标题? thx提前。

3 个答案:

答案 0 :(得分:2)

db.r - >评级 db.m - >电影

聚合框架附带了3.2版本的$ lookup - 这是建议解决方案的核心部分。

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android'.
> Cannot evaluate module google-play-services_lib : Configuration with name 'debug' not found.

欢迎任何彗星!

答案 1 :(得分:1)

你可以这样做:

db.moviesCollection.aggregate([
    {
        $match: { 
            "mid": {
                $in: db.ratingsCollection.find().map(function(r) { return r.mid; })
            }
        }
    }
])

答案 2 :(得分:-3)

select * from A where exists (select mid from B where B.mid = A.mid)
相关问题