【问题标题】:How to perform joins operation on mongoDB using java [duplicate]如何使用java在mongoDB上执行连接操作[重复]
【发布时间】:2017-10-11 13:11:47
【问题描述】:

我有两张桌子

1.部门集合 2.员工收藏
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 中执行连接

【问题讨论】:

  • 您必须提供您的尝试,并指导我们解决特定问题。

标签: java mongodb


【解决方案1】:

这将产生您所寻求的输出。请注意,通过将as 字段设置为employeeList,它会用扩展的“加入”数据覆盖原始列表:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2023-03-20
    相关资源
    最近更新 更多