티스토리 뷰

반응형

# $look를 이용한 LINK


>> 2개 COLLECTION LINK

db.employees.aggregate(

{$project: {empno:1, ename:1, job:1, deptno: 1}

},

{$lookup: {from: "department", 

           localField:"deptno",

           foreignField: "deptno",

           as : "depart_Info"

           }

},

{$sort: {empno:-1}}

).pretty()


>> 3개 COLLECTION LINK

db.employees.aggregate(

{$project: {empno:1, ename:1, job:1, deptno: 1}

},

{$lookup: {from: "department", 

           localField:"deptno",

           foreignField: "deptno",

           as : "depart_Info"

           }

},

{$unwind:"$depart_Info"},

{$lookup:{from: "location", 

           localField:"depart_Info.locno",

           foreignField: "locno",

           as : "locat_Info"

           }

},

{$project:{_id:0,

          empno: 1,

          ename: 1,

          job: 1,

          "depart_Info.deptno": 1,

          "depart_Info.dname": 1,

          "locat_Info.lname": 1

         }

}


).pretty()


>> $out 연산자를 이용, link 결과를 collection으로 저장

db.employees.aggregate(

{$project: {empno:1, ename:1, job:1, deptno: 1}

},

{$lookup: {from: "department", 

           localField:"deptno",

           foreignField: "deptno",

           as : "depart_Info"

           }

},

{$unwind:"$depart_Info"},

{$out:"emp_dept"}

)


db.emp_dept.find().pretty()






- EOF -



반응형