在Mongoid中将LIKE/regex与变量一起使用 [英] Use LIKE/regex with variable in mongoid

查看:79
本文介绍了在Mongoid中将LIKE/regex与变量一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找文本中包含单词test的所有文档.以下工作正常:

I'm trying to find all documents whose text contains the word test. The below works fine:

@tweets = Tweet.any_of({ :text => /.*test.*/ })

但是,我希望能够搜索用户提供的字符串.我以为下面的方法可以工作,但不能:

However, I want to be able to search for a user supplied string. I thought the below would work but it doesn't:

searchterm = (params[:searchlogparams][:searchterm])
@tweets = Tweet.any_of({ :text => "/.*"+searchterm+".*/" })

我已经尽力想尽一切办法,任何人都知道我可以做些什么来完成这项工作.

I've tried everything I could think of, anyone know what I could do to make this work.

谢谢.

推荐答案

searchterm = (params[:searchlogparams][:searchterm])
@tweets = Tweet.any_of({ :text => Regexp.new ("/.*"+searchterm+".*/") })

searchterm = (params[:searchlogparams][:searchterm])
@tweets = Tweet.any_of({ :text => /.*#{searchterm}.*/ })

这篇关于在Mongoid中将LIKE/regex与变量一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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