Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb regex searching issue in meteor

my code is

var search_element=Session.get("search_string");
var adc="/"+search_element+"*/";
console.log(adc);
return Polls_Coll.find({question:{$regex:adc}});

Why it is not working

if i give

Polls_Coll.find({question:{$regex:/Best*/}});

in conslow it's working and if i substitute the regex with the value(search_element) it is not working. I think it is replacing like this

Polls_Coll.find({question:{$regex:"/Best*/"}});(with quotes "/Best*/")

Is that the main problem? or Is there any silly mistake i did??

like image 841
Sasikanth Avatar asked Feb 17 '14 08:02

Sasikanth


1 Answers

There are two types of syntax:

Polls_Coll.find({question:/Best*/});

and

Polls_Coll.find({question:{$regex:"Best*"}});

You've used elements in each so thats probably why its not working. There's a bit more details about this at the mongodb docs: http://docs.mongodb.org/manual/reference/operator/query/regex/

Both forms work fine in Meteor, so you shouldn't have a problem with them.

like image 65
Tarang Avatar answered Oct 23 '22 22:10

Tarang