开发者

MongoDB Regular Expression: Contains an e-mail inside a string

开发者 https://www.devze.com 2023-03-04 16:31 出处:网络
Given that I have a string such as \"Donald Trump <donald@trump.com>\" \"Art Job <art@job.com>\"

Given that I have a string such as

"Donald Trump <donald@trump.com>" 
"Art Job <art@job.com>"

How can I find every row that contains "do开发者_开发百科nald@trump.com" using MongoDB in Rails?

Thanks


From the documentation:

search_string = params['search']

# Constructor syntax coll.find({"name" => Regexp.new(search_string)})

# Literal syntax coll.find({"name" => /#{search_string}/})

Beware though... You can't use an index with this query since your regex isn't anchored to the front of the string.


You can perform such a search only using regular expressions:

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

In your case you won't be able to search in a efficient way except you denormalize your data and store your email addresses in a dedicated attribute of the document.

No idea about Ruby but every driver should have support for regular expressions.

0

精彩评论

暂无评论...
验证码 换一张
取 消