Laravel与MongoDB连接的``喜欢''查询 [英] Laravel 'like' query with MongoDB connection

查看:87
本文介绍了Laravel与MongoDB连接的``喜欢''查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel'like'查询中遇到问题.我在laravel上有一个MIS,在MongoDb上有数据库.现在我的数据库中有一个名为 kw 的表,其中包含诸如 cars%20in%20London 之类的urlencoded关键字,现在我的查询给出了 cars cars%20in%20London ,但是当我搜索 cars%20in 时,我得到0个结果!这是在查询中使用laravel'like'的方式,但是Mongo使用/.m./形式,我该如何使其工作.这是我的模型函数

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

推荐答案

由于mongoDb中没有"like"之类的东西,我寻找了Mongodb regex,但是mongoDB的laravel regexp起到了吸引力,这是有效的查询. http://jenssegers.be/projects/laravel-mongodb

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

$search_volume_unprocessed = searchVolume::where('kw','=',$keyword)->orwhere('kw','regexp',"/.*$keyword/i")->where('status','=',1)->paginate(20);

这篇关于Laravel与MongoDB连接的``喜欢''查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆