我有一个名为Team
的收藏。样本文档如下所示-
{
"_id" : "TEA000000006",
"name" : "Team 1rrr",
"teamMembers" : [
{
"_id" : "1",
"employeeRef" : "000000",
"teamRoleRef" : "TMR000000012"
},
{
"_id" : "2",
"employeeRef" : "27375",
"teamRoleRef" : "TMR000000012"
},
{
"_id" : "3",
"employeeRef" : "15607",
"teamRoleRef" : "TMR000000012"
},
{
"_id" : "4",
"employeeRef" : "52321",
"teamRoleRef" : "TMR000000012"
},
{
"_id" : "5",
"employeeRef" : "000000",
"teamRoleRef" : "TMR000000016"
},
{
"_id" : "6",
"employeeRef" : "27375",
"teamRoleRef" : "TMR000000016"
},
{
"_id" : "7",
"employeeRef" : "15607",
"teamRoleRef" : "TMR000000035"
},
{
"_id" : "8",
"employeeRef" : "52321",
"teamRoleRef" : "TMR000000035"
}
]
}
我还有一个收藏集-TeamMember
。像这样的文档-
{
"_id" : "1",
"teamId" : "TEA000000006",
"employeeRef" : "000000",
"memberDisplayName" : "Anonoymous",
"memberTitle" : "BPO",
"memberLocation" : "Earth, Milkyway, Universe",
"memberBusinessPhone" : "123456789",
"memberEmail" : "anon@anon",
"teamRoleRef" : "TMR000000012",
"teamRoleSequence" : "4",
"roleName" : "Analyst"
}
在搜索Team
时,我需要通过键TeamMember
将Team
集合加入到teamRoleRef
中。
我该如何做以及在MongoDB v3.4和Java MongoDB驱动程序(v2.0.6.RELEASE)中对此进行编码
答案 0 :(得分:1)
db.Team.aggregate([
{"$unwind" : "$teamMembers"},
{"$project" : {"teamRoleRef" : "$teamMembers.teamRoleRef"}},
{"$lookup" : {
"from" : "TeamMember",
"localField" : "teamRoleRef",
"foreignField" : "teamRoleRef",
"as" : "teamMember_docs"
}}])
Java驱动程序 Refer this link