16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails: mongoid を利用したwhere filter

Last updated at Posted at 2013-02-19

rails で mongo を利用する為に、mongoidを利用した場合

Gemfile
gem "mongoid"
user.rb
class User 
  include Mongoid::Document
  field :user_id, :type => Integer
  field :name, :type => String
  field :email, :type => String
  field :time, :type => DateTime
end

######例:直近一週間にログインしたuserの情報

xxx.rb
User.where(:logged_in_time => { '$lte' => Time.now  , '$gt' => Time.now - 7.days })

######例:emailがnil以外の物

xxx.rb
 User.excludes(:email => nil)

######例:配列の中の値を使う

xxx.rb
User.any_in(:user_id => ids)

######例:like あいまい検索 (gmailのuserを探す)

xxx.rb
User.any_of({ :email =>/.*gmail.*/ })
16
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?