I am working with mongo DB creating a RESTful API. In the get request I want to filter documents
from two fields, ether from "name" or "category". For that I am using the $or and $regex operator together. The $regex operator work fine when using without the $or operator. But when the $or operator added it gives this error. MongoServerError: $regex has to be a string. Please help me. Here is my app.js code
app.route("/product")
.get(function(req, res){
var queryStr = req.query;
Product.find( { $or: [
{ name:{ $regex: queryStr.keyword, $options: 'i'}},
{category: {$regex: queryStr.category, $options: 'i'} }
]}, function(err, docs){
if(!err){
res.send({
success: true,
count: docs.length,
docs
})
} else {
res.send(err.stack)
}
})
})