如何在MongoDB聚合函数中进行LEFT JOIN?

时间:2019-06-20 01:49:33

标签: mongodb

我正在尝试使用聚合函数从引用的collection中获取特定值。但是,只有在主集合具有用于引用的字段(如RDBMS中的JOIN)时才选择记录,如下所示,

movies集合结构:

{
    "_id" : ObjectId("5cd9624c57fda41d20f925a4"),
    "name" : "Game over"
}
{
    "_id" : ObjectId("5cf3808e096bc30017e1ffae"),
    "name" : "Kolaigaran"
}
{
    "_id" : ObjectId("5cf229902b18930dba0488ee"),
    "name" : "Devi 2"
}

events集合结构:

{
    "_id" : ObjectId("5d0a1c9c568edb055ff850bd"),
    "movieId" : ObjectId("5cd9624c57fda41d20f925a4"),
    "resources" : {
        "posterLinks" : [ 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/e8brnxqxb7e6djvpvnrd.jpg", 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/vgsfwnwhykaqhov5zg2b.jpg"
        ],
        "youtubeLinks" : [ 
            "https://www.youtube.com/watch?v=ocnzgwjXdLw", 
            "https://www.youtube.com/watch?v=oLRHNYQ-Dno"
        ]
    },
}
{
    "_id" : ObjectId("5d0a201b568edb055ff850c0"),
    "movieId" : ObjectId("5cd9624c57fda41d20f925a4"),
    "resources" : {
        "posterLinks" : [ 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/qivg8ft6riul8rpfwygy.jpg"
        ]
    },
}
{
    "_id" : ObjectId("5d0a3a50568edb055ff850c1"),
    "movieId" : ObjectId("5cf3808e096bc30017e1ffae"),
    "resources" : {
        "posterLinks" : [ 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/egefgqzddu0jgj0wyagh.jpg", 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/qlnxslo7zobtm3tiqgor.jpg"
        ]
    },
}

这是我正在使用的查询

db.getCollection('movies').aggregate([
    {
       $lookup:
         {
            from: "events",
            let: { movieId: "$_id" },
            pipeline: [ 
                { $match: { $expr: { $eq: ["$movieId", "$$movieId"] } } },
                { $project: { resources: { $objectToArray: "$resources" } } },
                { $unwind: "$resources" },
                { $unwind: "$resources.v" },
                { $group: { _id:"$resources.k", "v": { $addToSet:"$resources.v" } } },
                { $group: { _id:null, resources: { $push: { k:"$_id", v:"$v" } } } },
                { $project: { resources: { $arrayToObject: "$resources" } } },
                { $replaceRoot: { newRoot:"$resources" } }
            ],
            as: "resources"
         }
    },
    { $unwind: "$resources" }
]);

这将返回movies集合中与events集合具有关联的所有文档,如下所示,

{
    "_id" : ObjectId("5cd9624c57fda41d20f925a4"),
    "name" : "Game over",
    "resources" : {
        "youtubeLinks" : [ 
            "https://www.youtube.com/watch?v=oLRHNYQ-Dno", 
            "https://www.youtube.com/watch?v=ocnzgwjXdLw"
        ],
        "posterLinks" : [ 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/qivg8ft6riul8rpfwygy.jpg", 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/vgsfwnwhykaqhov5zg2b.jpg", 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/e8brnxqxb7e6djvpvnrd.jpg"
        ]
    }
}
{
    "_id" : ObjectId("5cf3808e096bc30017e1ffae"),
    "name" : "Kolaigaran",
    "resources" : {
        "posterLinks" : [ 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/qlnxslo7zobtm3tiqgor.jpg", 
            "https://res.cloudinary.com/beinspired/image/upload/run-up/posters/egefgqzddu0jgj0wyagh.jpg"
        ]
    }
}

如您所见,它在电影中没有第三个文档,因为它在事件集合中没有任何关系。

这是我通过修改查询中的$match部分(如下所示但不起作用)而尝试过的方法,

{ $match: { $or: [ { $expr: { $eq: ["$movieId", "$$movieId"] } }, { resources: { $exists: false } } ] } }

我不确定$match是否应该更改为包括其他结果的确切部分。

对此的任何帮助将非常有用并非常感谢。预先感谢。

0 个答案:

没有答案