LIKE command in MongoDB (mongomapper)

how to use filter data like sql do "LIKE" in MongoDB, instead i use gem mongomapper in my rails applications? .thanks

+4
source share
3 answers

If you are looking for partial string matches, you can request a regular expression. Here is the relevant part of the mongomapper document:

http://api.mongodb.org/ruby/current/file.TUTORIAL.html#Querying_with_Regular_Expressions

It is worth noting this from the Mongodb docs:

"For simple prefix queries (also called root regular expressions), such as the / ^ / prefix, the database will use the index when it is available and suitable (like most SQL databases that use indexes to express the% LIKE prefix). Works only if there is no I in the flags (case insensitive).

+4
source

The closest thing to SQL LIKE is / query /

eg: -

Person.where('name' => /John/).all => John F, John Doe, Johnny...etc 

Edit: this is still case sensitive

+3
source

try it, it will work for me:

@store_array = User.where (: $ or => [{: first_name => /.#{@search_textasket./i}, {: last_name => /.#{@search_textβ–Ί ();

-1
source

All Articles