我有两张桌子
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"]
}
执行mongo db连接后,我需要以下格式的数据
{
_id:"3",
name: "XYZ Department",
employeeList:[
{
_id:"EMP1",
name:"Arjun Singh",
designation:"developer"
},
{
_id:"EMP2",
name:"Hitesh",
designation:"VTS support"
}
]
}
请帮我讲解如何在mongoDb中执行联接
答案 0 :(得分:0)
这将产生您所寻求的输出。请注意,通过将as
字段设置为employeeList
,它会使用展开的"加入"覆盖原始列表。数据:
db.department.aggregate([
{$lookup: { from: "employee", localField: "employeeList", foreignField: "_id", as: "employeeList"}}
]);