繁体   English   中英

维护被阻止的用户列表并过滤被阻止的用户

[英]Maintaining Blocked user list and filtering with blocked users

我正在开发一个人才搜寻应用程序,因为我在后端使用带有 mongoose 的节点。 所以,我需要添加阻止/取消阻止用户功能。 如果我将一个用户添加到另一个用户的阻止列表中,那么这两个用户不应出现在搜索、用户列表等中。

我要在以下区域进行过滤,

  • 获取所有用户的用户列表 api,
  • 搜索模块搜索特定用户

就像如果我阻止一个用户,该用户不应该携带我的任何回复。

我的用户 model

{
    _id: 1,
    name: "User One",
    email: "userone@email.com",
    mobile: "1234567890",
    gender: "male"
}

人才model

{
  _id: 1,
  categoryId: 1,
  experience: "good"
}

我的类别 model

{
   _id: 1,
   categoryId: 1,
   experience: "good"
}

我有一个过滤器 function 如下所示

users = await User.aggregate([
            conditions,
            {
                $lookup: {
                    from: 'talents',
                    as: 'talents',
                    let: { userId: '$_id' },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        {
                                            $eq: ['$$userId', '$userId'],
                                        },
                                        {
                                            $in: ['$category', cat],
                                        },
                                    ],
                                },
                            },
                        },
                    ],
                },
            },
            {
                $match: {
                    talents: { $ne: [] },
                },
            },
        ]);

因此,如果我像这样在我的用户 model 中创建一个嵌入式文档,

{
    _id: 1,
    name: "User One",
    email: "userone@email.com",
    mobile: "1234567890",
    gender: "male",
    blockedUsers: [{
       userId: 5,
       userId: 6
    }]
}

如果用户 1 使用上述代码过滤人才,结果不应包含用户 6 和 6。

这个怎么做????

您必须执行 2 个查询,

Select 当前用户预订用户 id:

let currentUserID = 1;
let currentUser = await User.findOne(
  { _id: currentUserID },
  { blockedUsers: "$blockedUsers.userId", _id: 0 }
)

操场

用户列表,并从列表中排除我被阻止的用户,我已更正您的查询,

let users = await User.aggregate([
  {
    $match: {
      isAdminApproved: 1,
      gender: { $in: ["male"] },
      _id: { $nin: currentUser.blockedUsers }
    }
  },
  {
    $lookup: {
      from: "talents",
      as: "talents",
      let: { userId: "$_id" },
      pipeline: [
        {
          $match: {
            category: { $in: [1] },
            $expr: { $eq: ["$$userId", "$userId"] }
          }
        }
      ]
    }
  },
  { $match: { talents: { $ne: [] } } }
])

操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM