Reputation: 317
I have two mongodb tables named PERSON and DOCUMENTS. I want to get the personName from the PERSON table whose IDs are in the DOCUMENT table. In relational tables query is like this.
SELECT personName from PERSON where personID in (SELECT personID from ID)
I tried to use aggregate and $lookup functions but it didn't get me the answer I want. The mongo query I used is,
db.PERSON.aggregate([
{
"$lookup": {
"from": "DOCUMENTS",
"localField": "personID",
"foreignField": "personID",
"as": "grp"
}
}
])
This mongo query joins both table and return every fileds. but I want only the personName.
Any help will be appreciated.
Upvotes: 3
Views: 1537
Reputation: 317
I found out a way to do it,
db.PERSON.aggregate([
{
"$lookup": {
"from": "DOCUMENTS",
"localField": "personID",
"foreignField": "personID",
"as": "grp"
}
},
{"$unwind" : "$grp"},
{"$project" : {
"personName" : 1
}
])
Upvotes: 1