“LIKE” command in MongoDB(mongomapper)

假装没事ソ 提交于 2019-12-22 08:55:53

问题


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

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