Q: MongoDB Full Text Search after joining 2 collections through a foreign key I have a collection called Team. A sample document looks like this - 
{ 
"_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 - 
{
"_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.
How do I do this and code for this in MongoDB v3.4 and Java MongoDB Driver (v2.0.6.RELEASE)

A: 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
