问题
I am facing an issue in laravel 'like' query. I have a MIS on laravel with databases on MongoDb. Now my DB has a table named kw
with urlencoded keywords like cars%20in%20London
, Now my query gives accurate results for cars
or cars%20in%20London
but when I search cars%20in
I get 0 results! This is how laravel 'like' is used in query but Mongo uses /.m./ form, How can I make this working. Here is my Model function
public static function selectKeywordIncomplete($keyword) {
$search_volume_incomplete = searchVolume::where('kw','like','%'.$keyword.'%')->orwhere('kw','=',$keyword)->where('status','=',1)->paginate(20);
return $search_volume_incomplete;
}
回答1:
well as there is no such thing as 'like' in mongoDb, I looked for Mongodb regex, but laravel regexp for mongoDB worked as a charm, here is the query which worked. http://jenssegers.be/projects/laravel-mongodb
$search_volume_unprocessed = searchVolume::where('kw','=',$keyword)->orwhere('kw','regexp',"/.*$keyword/i")->where('status','=',1)->paginate(20);
来源:https://stackoverflow.com/questions/32998013/laravel-like-query-with-mongodb-connection