8

How would I do this in mongoose, I could not find anything with an "or"

userModel.where('email')
         .equals(req.body.email)
         .or('username').equals(req.body.username) //need the "or"
         .exec(function (err, docs) {

  ....

  });
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
Michael W Riemer Jr
  • 531
  • 2
  • 8
  • 16

1 Answers1

22

I believe you might want to have a find query like so:

userModel.find({$or: [{email: req.body.email}, {username: req.body.username}]})
    .exec(function (err, docs) {
    ...
});

docs will then contain all documents in the userModel collection whose email field equals the one specified in the request body OR whose username field equals the one specified in the request body.

Florian
  • 712
  • 5
  • 18