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) {
....
});
The or operator is the counterpart to the and operator and is essential knowledge when working with database queries. We'll specifically show you how to use within mongoose.
The returned value could be an array of documents, a single document if it matches only one, or an empty array if no document is matched.
The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ).
For comparison of different BSON type values, see the specified BSON comparison order. If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (for example, <value1> , <value2> , and so on).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With