Match a value with multiple fields using OR in MongoDB
I have a scenario where I need to allow users to log into an application using either username or email address. Hence I need to match username with both username and email in users collection in a MongoDB.
Advertisements
To achieve I used $or condition to match two fields at once with findOne filter.
var user = await users.findOne({ $or: [ { username: req.body.username }, { email: req.body.username } ] });