Reputation: 263
Why is nothing returned when using $match
?
{
$lookup: {
from: 'users',
as: 'user',
let: {
"blogIds": "$blog.id"
},
pipeline: [{
$project: {
id: 1,
user_name: 1,
picture: 1,
blogs: 1
},
},
{
$match: {
$expr: {
$in: ["$$blogIds", "$blogs"]
}
}
}
]
}
}
If I remove $match
and add $addFields
, I get the following result:
{
$lookup: {
from: 'users',
as: 'user',
let: {
"blogIds": "$blog.id"
},
pipeline: [{
$project: {
id: 1,
user_name: 1,
picture: 1,
blogs: 1
},
},
{
$addFields: {
field: "$$blogIds"
}
},
]
}
}
Document example:
Upvotes: 1
Views: 28
Reputation: 57105
Problem - blog.id
is array, but you need only value.
So extract the value and use
Option 1
let: {
"blogIds": { $arrayElemAt: [ "$blog.id", 0 ] }
}
Option -2
{
$match: {
$expr: {
$in: [ { $arrayElemAt: [ "$$blogIds", 0 ] }, "$blogs"]
}
}
}
Upvotes: 3