I have two collections:
Users:
{
_id: ObjectId(\'5e11d2d8ad9c4b6e05e55b82\'),
name:"vijay"
}
Followers :
{
_id:ObjectId(\'5ed0c8faac47af698
You should use $not
$in
with $expr expression, Because $nin is a query operator not for aggregation expression,
let: { following: "$following"}
and use inside pipeline $$following
, because lookup pipeline will not allow to access fields without reference, {
$lookup: {
from: "Users",
let: {
following: "$following"
},
pipeline: [
{
$match: {
$expr: {
$not: {
$in: [
"$_id",
"$$following"
]
}
}
}
}
],
as: "result"
}
}
Working Playground: https://mongoplayground.net/p/08OT6NnuYHx