gpt4 book ai didi

javascript - 不等于运算符不能与 Mongoose 中的 _id 一起使用

转载 作者:行者123 更新时间:2023-12-02 21:54:59 25 4
gpt4 key购买 nike

我想对用户进行简单的过滤,以便返回的用户数组不包含我拥有其 _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,
});
},

值:

{
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”是一个包含当前用户信息的对象。因此尝试在过滤器中仅使用 me.id

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

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

关于javascript - 不等于运算符不能与 Mongoose 中的 _id 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60026908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com