问题
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