I want to use add regex to my query in mongodb.
server.js
app.post('/form',function(req,res){
var tf = req.body.termFound;
var str1 ='"keyword":{$regex:/'+tf+'/i}';
db.collection('a').find({str1}).toArray(function (err,results){
if (err) return console.log(err)
res.render('stest.ejs',{arr:results})
});
For example if the termFound is LOOPS. I wish to add the regex so that it will become non case sensitive, allowing me to find field values with (loops) or (loops,java)
"keyword":{$regex:/LOOPS/i}
Which is done from the line 3 code above. However when i try to send it to my mongoDB for connection. I suspect it gives a different value. As i tried
var str1 ='"keyword":{$regex:/'+tf+'/i}';
var str3 ={str1};
res.send(str3);
i was return with
{"str1":"\"keyword\":{$regex:/LOOPS/i}"}
My final query should be something like
db.collection('a').find({"keyword":$regex:/LOOPS/i}).toArray(function (err,results)
I have experimented with JSON.parse(str1)
var str1 ='"keyword":{$regex:/'+tf+'/i}';
var filters = JSON.parse(str1);
db.collection('a').find(filters).toArray(function (err,results){
but was given
SyntaxError: Unexpected token : in JSON at position 9
at JSON.parse ()
I have spent days trying to fix this.. my question is how can i query do this with regex so as to make my search non case sensitive?