How to Make a Lookup connection between two Collection

限于喜欢 提交于 2020-01-25 00:21:05

问题


Goal:
This sql and its result should be the same result from mongoDB's query code.
In order words, same result but for mongoDB.

Problem:
How to you make a lookup connection in relation to People and Role in Mongo DB's query code?

Info:
I'm new in mongo DB

SQL code 
    SELECT 
        a.*,
        '.' AS '.',
        b.*,
        '.'  AS '.',
        c.*
    FROM 
        [db1].[dbo].[People_Course_Grade] a
        INNER JOIN [db1].[dbo].[People] b on a.PeopleId = b.PeopleId
        INNER JOIN [db1].[dbo].[Role] c on b.RoleId = c.RoleId

Json data:

Role:  
[{"RoleId":1,"Name":"Student"},{"RoleId":2,"Name":"Teacher"}]

People_Course_Grade:   
[{"People_Course_GradeId":1,"PeopleId":1,"CourseId":1}, 
 {"People_Course_GradeId":2,"PeopleId":2,"CourseId":1}, 
 {"People_Course_GradeId":3,"PeopleId":3,"CourseId":2}, 
 {"People_Course_GradeId":4,"PeopleId":1,"CourseId":2}]

Course:
[{"CourseId":1,"Name":"Java"},{"CourseId":2,"Name":"Java II"}, 
 {"CourseId":3,"Name":"Statistik 1"}]

db.People_Course_Grade.aggregate([

    {
        $lookup:{
            from: "People",            
            localField: "people_id",   
            foreignField: "_id",       
            as: "people"               
        }
    },
    {   $unwind:"$people" },   


    {   
        $project:{
            course_id : 1,
            people_id : 1,
      //      grade_id : 1,
            Name : "$people.Name",


        } 
    }

]);

回答1:


You need to start with double $lookup since you have three collections. Then you can use $arrayElemAt to always get single element from lookup's result. To flatten your structure you can use $replaceRoot with $mergeObjects (promotes all the fields from people and course to root level.

db.People_Course_Grade.aggregate([
    {
        $lookup:{
            from: "Role",            
            localField: "PeopleId",   
            foreignField: "RoleId",       
            as: "people"               
        }
    },
    {
        $lookup:{
            from: "Course",            
            localField: "CourseId",   
            foreignField: "CourseId",       
            as: "course"               
        }
    },
    {
        $replaceRoot: {
            newRoot: {
                $mergeObjects: [
                    "$$ROOT",
                    { $arrayElemAt: [ "$people", 0 ] },
                    { $arrayElemAt: [ "$course", 0 ] },
                ]
            }
        }
    },
    {
        $project: {
            people: 0,
            course: 0
        }
    }
])

Mongo Playground

$arrayElemAt can always be replaced with $unwind like you tried. You also have a naming conflict on name field so probably you need to run $project to rename one of those fields - otherwise you'll get only one of them in final result.



来源:https://stackoverflow.com/questions/58671519/how-to-make-a-lookup-connection-between-two-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!