简体   繁体   中英

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

I'm trying to get the users from the mongo database using mongoose . If i select a user i want to eliminate that user records and get all the remaining users from database. how can i do that?

you can try to use the query below:-

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

Let me know if it helps. Thanks

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

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

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

Well, if you want to exclude just one user, you can use

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

And if you want to exclude more than one records. May be more than one selected records. This is the way to go

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

Here User is the user model. we will get all users except the current user

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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