Using the Mongoose $or operator
<p>How I can tell whether the username or the email in this scenario was the one that was found in the mongo database. I use <code>$or: [ { 'username': req.body.username}, {'email': req.body.email}]</code> to find an existing username or email but I need to know which one was found so I can send a message to the client to say the username is taken or the email is taken. I tried to log the "user" variable but it returned the object were it found the match.</p>

<pre><code>User.findOne({
    $or: [ { 'username': req.body.username}, {'email': req.body.email}]
}, function(err, user) {
    if (user) {
        console.log('username or password is taken');
    } else {
      userDetails.save(function(err) {
        if (err) throw err;
      });
    }
    if (err) {
        return done(err);
    }
});
</code></pre>