在MongoDB中筛选投影时使用$in

在MongoDB中筛选投影时使用$in,mongodb,projection,Mongodb,Projection,我有一个用户正在跟踪的ID数组,当我需要检查一个用户是否使用$eq运算符时,我可以投影元素 工作脚本如下所示: IsFollowing : { $filter : { input : '$Following', as : 'following', cond : { $eq : ['$$following.UserId', ObjectId('[requested user id]')] } } } 现在有一组用户ID,其中我只需要列表中的用

我有一个用户正在跟踪的ID数组,当我需要检查一个用户是否使用$eq运算符时,我可以投影元素

工作脚本如下所示:

IsFollowing : {
  $filter : {
    input : '$Following',
    as : 'following',
    cond : { 
      $eq : ['$$following.UserId', ObjectId('[requested user id]')]
    }
  }
}
现在有一组用户ID,其中我只需要列表中的用户ID:

{
    Following: {
        $filter : {
            input : '$Following',
            as : 'following',
            cond : { 
                $in : ['$$following.UserId', [ObjectId('[requested user´id]')....]]
            }
        }
    }
}
但我得到了这个mongo错误:

命令失败,错误为15999:“服务器上的“”中的运算符“$”无效

我不得不限制结果,因为一些用户有成千上万的人跟随他们


是否可以以任何方式仅提取以下对象ID有限数组中的元素?

$in
运算符仅允许在
$match
阶段使用。它只允许在启动Mongo 3.3.5版本的
$project
阶段中使用。您可以使用
$setisSubset
运算符执行类似的操作。类似于
cond:{$setIsSubset:[[['$$following.UserId'],[ObjectId('[requested user'id]')…]}