How to do simple mongoose findOne with multiple conditions? How to do simple mongoose findOne with multiple conditions? mongoose mongoose

How to do simple mongoose findOne with multiple conditions?


Try using the mongoose promise system (.exec()). The query inside .findOne() should be a single object.

User    .findOne({email: email, pwd: pwd})    .exec(function(err, user){        ...    });

Sidenote - it looks like this is for authenticating a a user login. It might be a better strategy to query just on the email, then try to match the passwords to give more depth to the error responses than a blanket 'Invalid login' type response.

User    .findOne({email: email}) //If not found, no user with that email exists    .exec(function(err, user){        var hashed_pwd = hashPassword(pwd);        if(!hashed_pwd === user.pwd){            //If they don't match, user entered wrong password        }        ...    });


User.findOne({ 'email': email, 'pwd': pwd }, function (err, user) {

this syntax is correct. if this isn't returning any results, it means you didn't match any records.

my guess is that your pwd field is hashed / encrypted and that you need to run the same hash / encryption on your pwd variable before doing this findOne


When the mongoDB is returning your docs, if it has not found Data then it will return null .You can check your docs if it is null then response with some other page.On the other hand if docs have data respond with the next page.

if(docs)    respond("your content");else  respond("error page incorrect credentials")

I was doing the mistake thinking that it will create an exception err, but it's not an error it is simply a null data.