Escape variables in mongodb query

Hey there :wave:

I want to escape a mongoDB query. In particular, I want to find substring in a textInput

I started here

{ "name": {{ textInput1.value !== "" ? textInput1.value : { $exists:true } }} }

but when I do something like that, to find substrings

{ "name": {{ textInput1.value !== "" ? "/"+textInput1.value+"/" : { $exists:true } }} }

Generates

{ "name": "/Lat/"  }

instead of

{ "name": /Lat/  }

Thanks in advance !!

Hey @Carlos_Narez!

I think the easiest solution for your problem is to use the $regex syntax instead of the /pattern/ syntax. You could rewrite your query like this to get the result you're looking for:

{
  "name": {{ textInput1.value != "" ? { $regex: textInput1.value, $options: 'i'} : {$exists: true} }} 
}
1 Like

Awesome dude, It works like a charm!

1 Like