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

199
Views
How Can I Pass A Variable e.g. /variable/ In a Mongo Query?

I'm trying to pass in searchTerm from a variable, how would I achieve this please?

const mongoquery = { description: { $in: [ /searchTerm/ ] } };

I initially tried with:

const mongoquery = { description: { $in: [ `/${searchTerm}/` ] } };

But, this doesn't work because the ` are included in the string:

{ description: { '$in': [ '/FAB/' ] } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use RegExp in searchTerm like as

const mongoquery = { description: new RegExp(searchTerm, "i") };

For like search you can use like as

const mongoquery = { description: new RegExp("^" + searchTerm + "$", "i") };

  OR 

const mongoquery = { description: new RegExp(searchTerm + "$", "i") };
about 4 years ago · Juan Pablo Isaza Report

0

You can use RegExp method,

const mongoquery = { description: { $in: [ new RegExp(searchTerm) ] } };
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!