Hi,
I have a collection Members and I am trying to make a join with the same collection to find all the patients of a staff member.
I have the following members:
[
{
"_id": "alice",
"type": "staff"
},
{
"_id": "john",
"type": "staff"
},
{
"id": "janice",
"type": "patient",
"careTeam": [
{
"_id": "alice",
"primary": true
}
]
},
{
"id": "michael",
"type": "patient",
"careTeam": [
{
"_id": "john",
"primary": true
},
{
"_id": "alice"
}
]
}
]
The issue is I need to add two conditions but I can’t. I need careTeam._id = $$mainId and careTeam.primary=true
I only want to receive the primary caseload of the staff not the whole caseload
If I do a lookup like this I receive both janice and michael but I only need janice
{
$lookup: {
from: "Members",
localField: "_id",
foreignField: "careTeam._id",
as: "caseLoad"
}
}
I tried
{
$lookup: {
from: "Members",
let: { primary: "_id" },
pipeline: [{$unwind:"$careTeam"},
{
$match: {
$expr: {
$and: [{ $eq: ["$careTeam._id","$$primary"] }],
},
},
}],
}
}
but nothing I get empty caseLoad, I also tried with $unwind but still nothing.
{
$lookup: {
from: "Members",
let: { primary: "_id" },
pipeline: [{$unwind:"$careTeam"},
{
$match: {
$expr: {
$and: [{ $eq: ["$careTeam._id","$$primary"] }],
},
},
}],
}
}
I know I still have to add { $eq: ["$careTeam.primary", true] } inside the mathc expr but first I need to receive back something in the caseLoad.
Can anyone help me out in what am I doing wrong?
Thank you for your help in advance,
Bogi