I have 2 collections :
Users collection :
{
_id: ObjectId
}
Toto Collection :
{
_id: ObjectId
fieldA: {_id: ObjectId}
fieldB: [{_id: ObjectId}, {_id: ObjectId}]
}
And I'm trying to join the element of A collection which fieldA's id OR one of fieldB's id is matching with user's id with following aggregation :
db.users.aggregate([
{
$lookup: {
from: "Toto",
let: { "user_id": "$_id" },
as: "fcollection",
pipeline: [
{ $match:
{$or:[
{"fieldB": {$elemMatch: {"_id": "$$user_id"}}},
{$expr: {$eq :["$fieldA._id", "$$user_id"]}}
]}
}
]
}
])
But in the output I'm just getting the document's where fieldA's id is matching but none of the fieldB's. What am I doing wrong?