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/' ] } }
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") };