I\'m trying to find all documents whose text contains the word test. The below works fine:
@tweets = Tweet.any_of({ :text => /.*test.*/ })
H
searchterm = (params[:searchlogparams][:searchterm])
@tweets = Tweet.any_of({ :text => Regexp.new ("/.*"+searchterm+".*/") })
or
searchterm = (params[:searchlogparams][:searchterm])
@tweets = Tweet.any_of({ :text => /.*#{searchterm}.*/ })
@tweets = Tweet.any_of({ :text => Regexp.new ("/.*"+searchterm+".*/") })
is work, but no result
@tweets = Tweet.any_of({ :text => Regexp.new (".*"+searchterm+".*") })
is ok and have result
@tweets = Tweet.any_of({ :text => /.*#{searchterm}.*/ })
is work, no result
My mongoid version is 3.1.3 and ruby version is 1.9.3
There is nothing wrong with the mongodb regex query. The problem is passing variable to ruby regex string. You cannot mix string with regex like normal strings
Instead
"/.*"+searchterm+".*/"
try this
>>searchterm = "test"
>>"/#{searchterm}/"
>> "/test/"
@tweets = Tweet.any_of({ :text => Regexp.new (".*"+searchterm+".*") })
is ok and have result
This worked for me by doing:
Model.find(:all, :conditions => {:field => /regex/i})