Mongoid 4: like query on integer column

那年仲夏 提交于 2019-12-11 09:29:44

问题


I need a mongoid search like query with INTEGER COLUMN. For example:

SELECT * FROM users WHERE mobile LIKE '%9980%';

Here is the my model:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  ##
  # Columns
  field :name,                      type: String
  field :mobile,                    type: Integer
end

I already tried following examples. But no luck :(

User.where(:$where => "/^#{params[:mobile]}/")
User.any_of({mobile: /.*#{params[:mobile]}.*/i})
User.where(mobile: /8801/))

How to write it with mongoid?


回答1:


Try this

User.where(mobile: /.*#{params[:mobile]}.*/i)



回答2:


SOLUTION:

users = User.where(:$where => "/^#{params[:mobile]}/.test(this.mobile)")



回答3:


User.where(:mobile => /\d*{params[:mobile]}\d*/)

Like function in Mongoid is just instead the arguement of where to regexp



来源:https://stackoverflow.com/questions/25739745/mongoid-4-like-query-on-integer-column

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