MongoDB Full Text Search after joining 2 collections through a foreign key

Multi tool use
Multi tool use


MongoDB Full Text Search after joining 2 collections through a foreign key



I have a collection called Team. A sample document looks like this -


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"
}
]
}



I have one more collection - TeamMember. A document from that looks like this -


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"
}



When searching through Team , I need to join the TeamMember collection to Team through the key teamRoleRef.


Team


TeamMember


Team


teamRoleRef



How do I do this and code for this in MongoDB v3.4 and Java MongoDB Driver (v2.0.6.RELEASE)




1 Answer
1


db.Team.aggregate([
{"$unwind" : "$teamMembers"},
{"$project" : {"teamRoleRef" : "$teamMembers.teamRoleRef"}},
{"$lookup" : {
"from" : "TeamMember",
"localField" : "teamRoleRef",
"foreignField" : "teamRoleRef",
"as" : "teamMember_docs"
}}])



$lookup



Java Driver Refer this link





We use MongoDB 3.4.
– Prateek Narendra
Jul 3 at 9:21





@senthur-dava But more importantly, how do I code the search in Java ?
– Prateek Narendra
Jul 3 at 9:43





This yields n records (where n are the number of team members in team). How do I deduplicate them them ?
– Prateek Narendra
Jul 4 at 5:12





What's your actual needed response post some sample?
– Senthur Deva
Jul 4 at 7:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Lnd089tUPD2SMKeS0ofE5XUyZ giA8m1LVs,GKME1CMR80JvcW
QvuVGNMcz,LRoGq55 z,UYXq4lq

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications