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

267
Views
MongoDB $match in regex does not work gives an error

I have a document like:

{
  id: string,
  blogCover:{ title: string }, 
  published: boolean
}

I am using $regex to create a search system as follows:

    app.post('/searchblogs',(req,res)=>{
    var searchfield = req.body.query;

    TFBlog.find({"blogCover.title":{
      $regex:searchfield,
      $options:'$i'
    }}).then((docs)=>{
     res.send(docs);
    });

    });

Now I want search results to only include documents with published:true

I used $match like

    TFBlog.find({"blogCover.title":{

      $match : { published : true },
      $regex:searchfield,
      $options:'$i'

    }}).then((docs)=>{
     res.send(docs);
    });

This gives an error :

(node:16304) UnhandledPromiseRejectionWarning: Error: Can't use $match with String.

What should I do ? is there any alternative to achieve this???

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

$match is a stage in aggregate() query, and it can not be used in find(). Change your code like this:

TFBlog.find({
  "blogCover.title": { $regex: searchfield, $options: 'i' },
  "published": true
}).then((docs) => {
  res.send(docs);
});
about 4 years ago · Juan Pablo Isaza Report
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!