Ruby on rails Mongoid 4:对整型列的like查询

Ruby on rails Mongoid 4:对整型列的like查询,ruby-on-rails,mongodb,mongoid,mongoid4,Ruby On Rails,Mongodb,Mongoid,Mongoid4,我需要一个带有整数列的mongoid类搜索查询。例如: SELECT * FROM users WHERE mobile LIKE '%9980%'; 以下是我的模型: class User include Mongoid::Document include Mongoid::Timestamps ## # Columns field :name, type: String field :mobile,

我需要一个带有整数列的mongoid类搜索查询。例如:

SELECT * FROM users WHERE mobile LIKE '%9980%';
以下是我的模型:

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

  ##
  # Columns
  field :name,                      type: String
  field :mobile,                    type: Integer
end
我已经试过下面的例子了。但是没有运气:(

如何使用mongoid编写它?

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

Mongoid中的Like函数只是将
where
的参数改为
regexp

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

Mongoid中的Like函数只是将
where
改为
regexp
试试这个

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

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

解决方案:

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

解决方案:

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

您好,谢谢您尝试帮助我。我运行了您的代码,但它不起作用。下面是命令:
User.where(:mobile=>/\d*8801\d*/).length
。它显示了0。但在我的db中它是32。哦,对不起,我忽略了
mobile
字段是一个
Integer
regexp
只能与
String
字段一起使用。为什么
mobile
字段使用整数类型,如果一个mobile是
18892887374
,那么大的整数。因为在我国的mobile number由8个字符组成。有什么办法吗?使用ruby方式
User.all.select{u | u.mobile.to|u.include?(params[:mobile])
Hmm,它将减少我的加载时间:(您好,谢谢您尝试帮助我。我运行了您的代码,但它不起作用。下面是命令:
User.where(:mobile=>/\d*8801\d*/).length
。它显示了0。但在我的db中它是32。哦,对不起,我忽略了
mobile
字段是一个
Integer
regexp
只能与
String
字段一起使用。为什么
mobile
字段使用整数类型,如果一个mobile是
18892887374
,那么大的整数。因为在我国的mobile number由8个字符组成。有什么办法吗?使用ruby方式
User.all.select{u | u.mobile.to_.include?(params[:mobile])}
Hmm,它将减少我的加载时间:(谢谢你试图帮助我,但它不起作用。我认为问题是
mobile
列是
Integer
。谢谢你试图帮助我,但它不起作用。我认为问题是
mobile
列是
Integer
。mobile?你将电话号码存储为号码吗?mobile?你将电话号码存储为n吗乌姆?