如何在 mongodb $lookup 聚合中使用 NOT IN 数组条件 [英] How to use NOT IN array condition inside mongodb $lookup aggregate

查看:100
本文介绍了如何在 mongodb $lookup 聚合中使用 NOT IN 数组条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合:

用户:{_id: ObjectId('5e11d2d8ad9c4b6e05e55b82'),名称:维杰"}追随者:{_id:ObjectId('5ed0c8faac47af698ab9f659'),user_id:ObjectId('5e11d2d8ad9c4b6e05e55b82'),下列的:[对象 ID(5ee5ca5fac47af698ab9f666'),ObjectId('5df7c0a66243414ad2663088')]created_at:2020-05-29T08:34:02.959+00:00"}

我需要从特定用户的用户表中列出不在以下数组中的所有用户,我通过在关注者表中编写聚合函数得出了以下内容.

<预><代码>[{'$匹配':{'user_id': new ObjectId('5e11d2d8ad9c4b6e05e55b82')}}, {'$项目':{'user_id': '$user_id','关注':'$关注'}}, {'$查找':{'来自':'用户','管道':[{'$匹配':{'_ID': {'$nin': ['$关注']}}}],'结果'}}]

但这并没有填充我需要的输出.

谁能帮我解决这个问题?

谢谢

解决方案

您应该将 $not $in$expr 表达式,因为 $nin 是一个查询运算符,不用于聚合表达式,

  • 您还需要使用 let: { following: "$following"} 创建变量并使用内部管道 $$following 创建变量,因为查找管道不会允许在没有引用的情况下访问字段,

<代码> {$查找:{来自:用户",让: {以下:$以下"},管道:[{$匹配:{$expr:{$不:{$in: [$_id",$$以下"]}}}}],如:结果"}}

工作游乐场:https://mongoplayground.net/p/08OT6NnuYHx

I have two collections:

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

Followers :
{
    _id:ObjectId('5ed0c8faac47af698ab9f659'),
    user_id:ObjectId('5e11d2d8ad9c4b6e05e55b82'),
    following:[
        ObjectId(5ee5ca5fac47af698ab9f666'),
        ObjectId('5df7c0a66243414ad2663088')
    ]
    created_at:"2020-05-29T08:34:02.959+00:00"
}

I need to list all users who are not in the following array from users table for a particular user, I've come up with the below by writing aggregate function in followers table.

[
  {
    '$match': {
      'user_id': new ObjectId('5e11d2d8ad9c4b6e05e55b82')
    }
  }, {
    '$project': {
      'user_id': '$user_id', 
      'following': '$following'
    }
  }, {
    '$lookup': {
      'from': 'users', 
      'pipeline': [
        {
          '$match': {
            '_id': {
              '$nin': [
                '$following'
              ]
            }
          }
        }
      ], 
      'as': 'result'
    }
  }
]

but this is not populating the output I needed.

Can anyone help me with this?

Thanks

解决方案

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

这篇关于如何在 mongodb $lookup 聚合中使用 NOT IN 数组条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆