Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb findOne with or [closed]

In my example i have a login view email or username can send one of two.

User.findOne({ 'email' :  email }, function(err, user) {
// awesome user
});

and i need something like this:

User.findOne({ 'email' :  email } || {'username' :  email}, function(err, user) {
// awesome user
});
like image 276
Honchar Denys Avatar asked Dec 14 '22 17:12

Honchar Denys


1 Answers

Can do using the $or query operator:

User.findOne({$or: [{'email': email}, {'username': email}]}, function(err, user) {
    // awesome user
});
like image 152
famousgarkin Avatar answered Jan 03 '23 07:01

famousgarkin