Problem finding mongodb regular expression in meteor

my code

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

Why does it not work

if i give

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

in conslow it works, and if I substitute regex with the value (search_element), it does not work. I think he replaces this as

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

Is this a major issue? Or is there a stupid mistake I made?

+3
source share
2 answers

There are two types of syntax:

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

and

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

You used elements in each, therefore, probably why it does not work. This is a bit more about this in the mongodb docs: http://docs.mongodb.org/manual/reference/operator/query/regex/

Both forms work fine in Meteor, so you should not have problems with them.

+10
source

I use this syntax and it works for me:

Polls_Coll.find({ name: {$regex: "adc", $options: '-i'} });

:

SELECT * FROM Polls_Coll WHERE name LIKE '%adc%'

https://atmospherejs.com/meteor/reactive-var , :

if (Meteor.isServer) {
    Meteor.publish('customersSearch', function(searchValue){
        if (searchValue) {
            return Customers.find({ name: {$regex: searchValue, $options: '-i'} });
        }
   });
}

var searchQuery = new ReactiveVar();

Template.NewRO.onCreated(function(){
    var self = this;
    self.autorun(function() {
        self.subscribe('vehicles');
        self.subscribe('customersSearch', searchQuery.get());
    });
});
+3

All Articles