NodeJS Query with MongoDB

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



NodeJS Query with MongoDB



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?




2 Answers
2



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)
)
);



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.


find



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


'/'+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)
);
)





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.
– Jeremy Wong
2 hours ago





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.
– wlh
9 mins ago


RegExp()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS