0

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?

asked Sep 23, 2018 at 22:54

2 Answers 2

1

Edit: Managed to find out the answer. Had to use new RegExp() to create the reg exp object!

app.post('/form',function(req,res){
 var tf=req.body.toFind;
 db.collection('a').find({"keyword":new RegExp(tf,'i')}).toArray(function (err,results){ 
 if (err) return console.log(err)
 res.render('stest.ejs',{question:results}) 
 }) 
}); 
answered Sep 24, 2018 at 0:09

Comments

0

You are so close here. Instead of trying to pass a string to your MongoDB find as you have above, just pass in the regex.

This way, you can use a template string or concatenate strings (such as '/'+tf+'/i').

UPDATE This method just mentioned won't work for all the reasons I'm getting ready to explain in the following paragraph regarding the original problem. This doesn't create an actual regular expression.

Previously, when you pass in a single string to the find function, Mongo starts looking for the key "str1" and seeing if it found a value of that string, whatever '"keyword":{$regex:/'+tf+'/i}' would evaluate to as a single string (note the surrounding single-quotes). You can't call JSON parse on this particular string because it is not a valid JSON encoded string. See an example below that should work better:

app.post('/form',function(req,res){
 var tf = req.body.termFound;
 // var reg =`/${tf}/i`; not an actual regex, still a string!!!
 var reg = new RegExp(tf, 'i') // as you have since discovered
 db.collection('a').find({"keyword": {$regex: reg }}).toArray(function (err,results){ 
 if (err) return console.log(err)
 res.render('stest.ejs',{arr:results}) 
 });
})
answered Sep 23, 2018 at 23:11

2 Comments

Tried the method you mentioned but not sure why it didn't work for me. Thanks for explaining to me the concept on the string too! Prompted me to try another direction.
Actually, mine doesn't work for the same reason your original doesn't work. Even with the template string, I have just created a String. Your discovered solution of created a new instance of RegExp() is spot on.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.