I'm building an application (MongoDB database) where user can register an account. The account model has 3 value's that should be unique: ID, Username & Email. I'm using Mongoose v5.5.5 for querying etc.
When someone wants to register a new account, I want to do a query (findOne or find) to see if there is a user with that ID OR username OR mail. I thought the code below would work.
let query = {
'id': sanitized.id,
'username': sanitized.username,
'mail': sanitized.mail
}
AccountModel.find(query, (error, data) => {
// Handle result
})
So lets say there is a user with as username user1 and as email user1@mail.com. Now another user wants to register user the username user2 but with the same email as user1.
How can I perform a query that matches on OR the username OR the mail so that I can tell either of those is already being used by another user?
I prefer using one single query to keep the code clean, but I couldn't find a solution yet. Hope someone can help.