“LIKE” command in MongoDB(mongomapper)

与世无争的帅哥 提交于 2019-12-05 16:01:46

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."

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

try these it work for me :

@store_array=User.where(:$or => [{:first_name => /.#{@search_text}./i}, {:last_name => /.#{@search_text}./i}]).all();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!