0

I need to find the items from MongoDb db using Mongoid gem in rack application. Here is what I have:

items = items.any_of(
                            {item_id: q},   #ok
                            {title: /q/},
                            {title2: /q/},
                            {code: q},      #ok
                            {seller: q}     #ok
                          )

I need to improve search by title and title2.

First off, how to make it not case sensitive? Secondly, if q contains many words, I want it to search by any word it contains.

How can I do that?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • Suggest http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like for the /.q./ syntax. And, add an /i http://stackoverflow.com/questions/1863399/mongodb-is-it-possible-to-make-a-case-insensitive-query – WiredPrairie Dec 31 '12 at 12:26
  • Thank you. What about "if q contains many words..."? – Alan Coromano Dec 31 '12 at 13:34
  • 1
    Do you mean, "This is my title" and you want to search for "my"? The first link should work for that. If not, you could search by breaking phrases into arrays and doing "$in", but you'll not get the "contains" style string matching and it would be case sensitive. You could write JavaScript to do the search in a $where, but it's not going to be fast as it brute-forces the entire collection of documents and executes JavaScript for each. Full text search "coming" to mongoDB: http://www.slideshare.net/mongodb/roadmap-15607515, or maybe: https://github.com/aaw/mongoid_fulltext – WiredPrairie Dec 31 '12 at 14:30

0 Answers0