How to use NOT IN array condition inside mongodb $lookup aggregate

前端 未结 1 735
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 23:46

I have two collections:

Users:
{
    _id: ObjectId(\'5e11d2d8ad9c4b6e05e55b82\'),
    name:"vijay"
}

Followers :
{
    _id:ObjectId(\'5ed0c8faac47af698         


        
相关标签:
1条回答
  • 2021-01-22 23:58

    You should use $not $in with $expr expression, Because $nin is a query operator not for aggregation expression,

    • one more fix you need to create variable using 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

    0 讨论(0)
提交回复
热议问题