【问题标题】:Not equal operator not working with _id in Mongoose不等于运算符不使用 Mongoose 中的 _id
【发布时间】:2020-02-03 01:03:22
【问题描述】:

我想对用户进行简单的过滤,以便返回的用户数组不包含登录用户,我有其_id。我尝试将stringObjectId 值都传递给$ne 字段,但根本没有应用该过滤器。我确信我通过了正确的_id。为什么这不起作用?

Query: {
    users: async (parent, { limit = 10 }, { models, me }) => {
      const filter = me
        ? { _id: { $ne: mongoose.Types.ObjectId(me.id) } }
        : {};
      return await models.User.find(filter, null, {
        limit,
      });
},

me值:

{
  id: '5e36eb8232c4082080f8288a',
  email: 'email0@email.com',
  username: 'user0',
  role: 'ADMIN',
  iat: 1580657981,
  exp: 1580675981
}

用户集合示例:

[
  {
    followersIds: [
      5e36ffe2c1d67534241968e7,
      5e36ffe2c1d67534241968ea,
      5e36ffe2c1d67534241968ed,
      5e36ffe2c1d67534241968f0,
      5e36ffe2c1d67534241968f3,
      5e36ffe2c1d67534241968f6,
      5e36ffe2c1d67534241968f9,
      5e36ffe2c1d67534241968fc,
      5e36ffe2c1d67534241968ff
    ],
    followingIds: [],
    _id: 5e36ffe2c1d67534241968e4,
    username: 'user0',
    email: 'email0@email.com',
    password: '$2a$10$vWAeJJ1blfCJOyAVrAN7c.a5e9HOFyU21GfQCkvbNTD3uC0lMgkta',
    name: 'Mrs. Fae Koelpin',
    bio: 'Repellat qui laudantium sed illum non corrupti optio. Sed perspiciatis ipsa nobis placeat tempore exercitationem rerum. 
Aut cupiditate omnis provident maxime reprehenderit laudantium.',
    avatarId: 5e36ffe2c1d67534241968e2,
    coverId: 5e36ffe2c1d67534241968e3,
    role: 'ADMIN',
    __v: 0
  },
  {
    followersIds: [],
    followingIds: [ 5e36ffe2c1d67534241968e4 ],
    _id: 5e36ffe2c1d67534241968e7,
    username: 'user1',
    email: 'email1@email.com',
    password: '$2a$10$enruHkMUkcw07T.vgxeEQ.BV0rJ7xKO7HwVYsqYRlM2Cl7y1oaLEa',
    name: 'Rasheed Hickle',
    bio: 'Distinctio dolorem asperiores deserunt debitis aut quia. Aut deleniti ea sit qui aliquid aperiam accusantium repellendus. Ut ut non nulla vel fugiat dolore.',
    avatarId: 5e36ffe2c1d67534241968e5,
    coverId: 5e36ffe2c1d67534241968e6,
    __v: 0
  },
  {
    followersIds: [],
    followingIds: [ 5e36ffe2c1d67534241968e4 ],
    _id: 5e36ffe2c1d67534241968ea,
    username: 'user2',
    email: 'email2@email.com',
    password: '$2a$10$juU/iUHy8g1vqBXmZplP8uiR75BPNm9ZfOoTpbzzEvhzNARRBFpKe',
    name: 'Jillian Kassulke',
    bio: 'Consequuntur molestiae repellat alias ut quis. Voluptas qui consectetur sed numquam fugit. Illo repellendus sint.',     
    avatarId: 5e36ffe2c1d67534241968e8,
    coverId: 5e36ffe2c1d67534241968e9,
    __v: 0
  }
]

【问题讨论】:

  • 你能发布me值吗?
  • 查看编辑后的帖子。
  • 您能否发布您的user 收藏的样本。它可以工作 mongoplayground.net/p/XdpFpiaAyJV 。顺便说一句:{limit,} 错误,应该是{limit : limit}(第二个限制有 10 个值)
  • 我发布了用户示例。
  • 可能你不小心传入了undefined,需要使用me._id

标签: javascript mongodb mongoose


【解决方案1】:

假设“我”是一个包含当前用户信息的对象。因此尝试在过滤器中仅使用me.id

{ _id: { $ne: me.id } }

{ _id: { $ne: ObjectId(me.id) } }

【讨论】:

  • 我都试过了,但还是不行。但是,当我设置 { username: { $ne: me.username } } 时,它正在工作。
猜你喜欢
  • 1970-01-01
  • 2012-01-10
  • 2019-03-19
  • 2011-05-04
  • 2012-09-03
  • 2016-07-29
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多