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.
精彩评论