问题
how to use filter data as sql do "LIKE" in MongoDB, instead I using gem mongomapper on my rails apps? .thanks
回答1:
If you're looking for partial matches on a string you can query with a regular expression. Here's the relevant part of the mongomapper docs:
http://api.mongodb.org/ruby/current/file.TUTORIAL.html#Querying_with_Regular_Expressions
Worth noting this from the Mongodb docs:
"For simple prefix queries (also called rooted regexps) like /^prefix/, the database will use an index when available and appropriate (much like most SQL databases that use indexes for a LIKE 'prefix%' expression). This only works if you don't have i (case-insensitivity) in the flags."
回答2:
the closest thing to SQL LIKE would be /query/
ex:-
Person.where('name' => /John/).all => John F, John Doe, Johnny...etc
Edit: this is still case sensitive
回答3:
try these it work for me :
@store_array=User.where(:$or => [{:first_name => /.#{@search_text}./i}, {:last_name => /.#{@search_text}./i}]).all();
来源:https://stackoverflow.com/questions/6806776/like-command-in-mongodbmongomapper