2017-10-11 37 views
0

加入對MongoDB的操作我有兩個表如何執行使用Java

 
1. Department collection 
2. Employee collection 

Both the table have unique _id. I have stored multiple employee ids on Department collection. 

Employee Collection: 
{ 
_id:"EMP1", 
name:"Arjun Singh", 
designation:"developer" 
}, 
{ 
_id:"EMP2", 
name:"Hitesh", 
designation:"VTS support" 
} 


Department Collection: 
{ 
_id:"3", 
name:"XYZ Department", 
employeeList:["EMP1","EMP2"] 
} 

後執行蒙戈DB連接我需要在以下格式的數據

{ 
_id:"3", 
name: "XYZ Department", 
employeeList:[ 
{ 
    _id:"EMP1", 
    name:"Arjun Singh", 
    designation:"developer" 
}, 
{ 
    _id:"EMP2", 
    name:"Hitesh", 
    designation:"VTS support" 
} 
] 
} 

請幫助我如何在mongoDb中執行連接

+0

你得提供你的企圖,並指導我們具體的問題。 – notyou

回答

0

這將產生您正在尋找的輸出。請注意,通過設置asemployeeList它與膨脹「加入」數據將覆蓋原來的列表:

db.department.aggregate([ 
{$lookup: { from: "employee", localField: "employeeList", foreignField: "_id", as: "employeeList"}} 
       ]);