Reputation:
I am trying to create a login system using mongoose to store the data. simply put... if a user model has both email and username fields, and i want to query the database to find if a new user signing up does not have the same email or username as an existing user?? basically something like this
const user = await User.findOne({ email: req.body.email || username: req.body.username })
if(user){
return "user already exists"
}
so basically i am asking how can i find if the email already exists in the database OR the username already exits in the database? Thank you for the help :}
Upvotes: 1
Views: 957
Reputation: 4801
You can use $or
operator, try:
const user = await User.findOne({ $or: [{ email: req.body.email }, { username: req.body.username }] })
if(user){
return "user already exists"
}
For your reference. Hope it helps.
Upvotes: 2