繁体   English   中英

如何使用mongoose从mongodb中获取除select用户之外的所有用户

[英]How to get all users except select user from mongodb using mongoose

我正在尝试使用mongoosemongo database获取用户。 如果我选择一个用户,我想消除该用户记录,并从数据库中获取所有其他用户。 我怎样才能做到这一点?

您可以尝试使用以下查询:-

UsersModel.find({ email: { $ne: 'testemail@email.com' } })

让我知道是否有帮助。 谢谢

UsersModel.find({ email: { $ne: 'testemail@email.com' } })

有关更多信息,请访问此网址

   https://docs.mongodb.com/manual/reference/operator/query/ne/

好吧,如果您只想排除一个用户,则可以使用

db.collection.find( { name: { $ne: "name" } } )

如果要排除多个记录。 可能是多个选定的记录。 这是要走的路

db.collection.find( { name: { $nin: ["name1", "name22"] } } )

这里的用户是用户模型。 我们将获得除当前用户外的所有用户

 const users = User.find({ _id: { $ne: user._id } })

暂无
暂无

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

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