Ruby‎ > ‎

Mongoid


MongoDB のRuby用O/R Mapper

MongoMapperもよく使われているみたいだけどMongoidの方がドキュメントがしっかりしていて使いやすそう。
Mongoid 2 を使うには最新版のMongoDBが必要。Mongoid 2でないとドキュメントに載っている機能(Validationとか)が使えなかったりする。

ドキュメント

class Person
  include Mongoid::Document
  include Mongoid::Timestamps  ## :created_at, :updated_at を自動設定

  field :first_name
  field :middle_initial
  field :last_name
  field :birthday, :type => Date
  field :blood_alcohol_level, :type => Float, :default => 0.0

  validates_presence_of :first_name, :message => ""
  validates_format_of :first_name, :with => /[A-Za-z]/
  validates_uniqueness_of :first_name
end

String以外は型を指定
Array, BigDecimal, Boolean, Date, DateTime, Float, Hash, Integer, String, Symbol, Time
Validationも使える

Create
person = Person.create(:first_name => "Syd", :last_name => "Vicious")

Save
person = Person.new(:first_name => "Syd", :last_name => "Vicious")
person.save # or person.upsert
saveはtrue/falseを返す。save!にすると失敗したとき例外を投げる。

Update
person = Person.new(:first_name => "Syd", :last_name => "Vicious")
person.update_attributes(:first_name => "Nancy")

Destroy
person = Person.create(:first_name => "Syd", :last_name => "Vicious")
person.destroy
person.delete   ##deleteはcallback(before_destroyとか)を無視する。

Find色々
Person.all(:conditions => { :first_name => "Syd" })
Person.find(:all, :conditions => { :first_name => "Syd" })

Person.first(:conditions => { :first_name => "Syd" }) ## 見つからなければnil
Person.find(:first, :conditions => { :first_name => "Syd" })

Person.last(:conditions => { :first_name => "Syd" })
Person.find(:last, :conditions => { :first_name => "Syd" })

Person.find_or_create_by(:first_name => "Syd")
Person.find_or_initialize_by(:first_name => "Syd")

find以外にも色んな便利な検索ができる
Person.all_in(:aliases => [ "Jeffrey", "The Dude" ])
Person.any_in(:status => ["Single", "Divorced", "Separated"])
Person.any_of({ :status => "Single" }, { :preference => "Open" })
Person.and(:age.gt => 18, :gender => "Male")
Person.where(:status => "Married").count
Person.excludes(:status => "Married")
Person.criteria.id("4b2fe28ee2dc9b5f7b000029")
Person.limit(20)
Address.near(:position => [ 37.7, -122.4, 10 ])
Person.not_in(:status => ["Divorced", "Single"])
Person.only(:first_name, :last_name)

Person.desc(:last_name).asc(:first_name)
Person.descending(:last_name).ascending(:first_name)
Person.order_by(:last_name.desc, :first_name.asc, :city.desc)
Person.order_by([[:last_name, :desc], [:first_name, :asc]])

Person.skip(100) ## offset
Person.where(:age.gt => 18, :gender => "Male")
Person.where(:last_name => /^Jord/)
Person.where(:age.gt => 18, :age.lt => 30)

合わせ技
Person.only(:first_name, :last_name).where("address.post_code" => "94133")
Person.only(:first_name).where("phones.country_code" => 1).in(:last_name => ["Vicious"])
Person.where(:last_name => "Zorg").and(:middle_initial => "J")
Person.where(:title.all => ["Sir"])
Person.where(:age.exists => true)
Person.where(:age.gt => 18)
Person.where(:age.gte => 18)
Person.where(:title.in => ["Sir", "Madam"])
Person.where(:age.lt => 55)
Person.where(:age.lte => 55)
Person.where(:title.ne => "Mr")
Person.where(:title.nin => ["Esquire"])
Person.where(:aliases.size => 2)
Person.where(:location.near => [ 22.5, -21.33 ])
Person.where(:location.within => { "$center" => [ [ 50, -40 ], 1 ] })

before_create
before_destroy
before_save
before_update
before_validation
after_create
after_initialize
after_destroy
after_save
after_update
after_validation


Comments