Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
Mongo DB regex in $or operator giving Error "regex has to be a string"

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)
                }    
            })

        }) 
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!