ActiveRecord vs Mongoid
-
Upload
ivan-nemytchenko -
Category
Technology
-
view
5.188 -
download
0
description
Transcript of ActiveRecord vs Mongoid
ActiveRecord vs Mongoid
Иван Немытченко, 7bits, Омск
PHP → Ruby on Rails →freelance →JazzCloud → Tulp → 7bits
2007 - Ruby on Rails
2007 - Ruby on Rails
2008 - HAML/SASS
2007 - Ruby on Rails
2008 - HAML/SASS
2010 - Coffeescript
→
2007 - Ruby on Rails
2008 - HAML/SASS
2010 - Coffeescript
2010 - Tequila RABL
2007 - Ruby on Rails 2008 - HAML/SASS 2010 - Coffeescript 2010 - Tequila → RABL
2011 - Tulp 2012 - MongoDB
2007 - Ruby on Rails
2008 - HAML/SASS
2010 - Coffeescript
2010 - Tequila RABL
2012 - ActiveRecord vs Mongoid
→
MongoDB
MongoDB is a scalable, high-performance, open
source NoSQL database“
”
НЕТ ТРАНЗАКЦИЙ
НЕТ ТРАНЗАКЦИЙ
НЕТ ДЖОЙНОВ
Таблицы → Коллекции
Таблицы → Коллекции
Строки → Документы
{ "_id" : ObjectId( "4fb8dd1324ec7c1344000003" ), "agent" : "Ivan Nemytchenko", "challenge" : "Introduce MongoDB documents", "status" : "accepted" }
{ "_id" : ObjectId( "4fb8dd1324ec7c1344000003" ), "agent" : "Ivan Nemytchenko", "missions": [ { "challenge" : "Introduce MongoDB documents", "status" : "done" }, { "challenge" : "Tell the truth about MongoDB", "status" : "considering" } ] }
Таблицы → Коллекции
Строки → Документы
Индексы → Индексы
MongoidObject Document Mapper
The philosophy of Mongoid is to provide a
familiar API to Ruby developers who have been
using Active Record or Data Mapper, while
leveraging the power of MongoDB's schemaless
and performant document-based design, dynamic
queries, and atomic modifier operations
“
”Durran Jordan, автор Mongoid
Связи
has_many
has_one
belongs_to
has_many
has_one
belongs_to
has_and_belongs_to_many
has_many
has_one
belongs_to
has_and_belongs_to_many
address:
{organization_ids:[1,2,3]}
organization:
{address_ids:[5,3,1,10]}
has_many
has_one
belongs_to
has_and_belongs_to_many
has_many :through
has_many
has_one
belongs_to
has_and_belongs_to_many
has_many :through
has_many
has_one
belongs_to
has_and_belongs_to_many
has_many :through
embeds_many
embeds_one
embedded_in
Lets Practice!
class Contact include Mongoid::Document field :type field :value embedded_in :placeend
place.contacts << Contact.new(:type => "phone", :value => "32-14-90")place.contacts << Contact.new(:type => "email", :value => "[email protected]")place.save
class Place include Mongoid::Document embeds_many :contactsend
{ "_id" : 1, "name" : "Планета Суши" "contacts" : [ { "_id" : 1, "type" : "phone", "value" : "32-14-90" }, { "_id" : 2, "type" : "email", "value" : "[email protected]" } ] }
{ "_id" : 1, "name" : "Планета Суши" "contacts" : [ { "_id" : 1, "_type" : "phone", "value" : "32-14-90" }, { "_id" : 2, "_type" : "email", "value" : "[email protected]" } ] }
class Contact include Mongoid::Document field :type field :value embedded_in :placeend
place.contacts << Phone.new(:value => "32-14-90")place.contacts << Email.new(:value => "[email protected]")place.save
class Place include Mongoid::Document embeds_many :contactsend
class Email < Contactend
class Phone < Contact end
Rating embedded_in :place
Place embeds_many :ratings
RatingType embedded_in :category
Category embeds_many :rating_types
Rating embedded_in :place
Place embeds_many :ratings
RatingType embedded_in :category
Category embeds_many :rating_types
Place{ "_id" : 1, "name" : "Планета Суши" "ratings" : [ { "_id":1, "type":"Качество обслуживания", "value":4 }, { "_id":2, "type":"Чистота", "value":5 } ] }
Rating embedded_in :place
Place embeds_many :ratings
RatingType embedded_in :category
Category embeds_many :rating_types
Place{ "_id" : 1, "name" : "Планета Суши" "ratings" : [ { "_id":1, "type":"Качество обслуживания", "value":4 }, { "_id":2, "type":"Чистота", "value":5 } ] }
Category{ "_id" : 1, "name" : "Рестораны" "rating_types" : ["Качество обслуживания", "Чистота"] }
Place.where(:"ratings.type"=>"Чистота", :"ratings.value.gt"=>3)
Place{ "_id" : 1, "name" : "Планета Суши" "ratings" : [ { "_id":1, "type":"Качество обслуживания", "value":4 }, { "_id":2, "type":"Чистота", "value":5 } ] }
Category{ "_id" : 1, "name" : "Рестораны" "rating_types" : ["Качество обслуживания", "Чистота"] }
Property embedded_in :place field :question field :answerPlace embeds_many :properties
Answer embedded_in :question
Question embedded_in :category embeds_many :answers
Category embeds_many :questions
>> category.questions << Question.new(:value => 'WiFi', :answers => ['есть', 'нету'])
>> place.properties << Property.new(:question => 'WiFi', :answer => 'есть')
Place{ "_id" : 1, "name" : ".." "contacts" : [ { "_type" : "phone", "value" : "32-14-90" }, { "_type" : "email", "value" : "..." }], "ratings" : [ { "type" : "..", "value" : ".." }, { "type" : "..", "value" : ".." }], "properties" : [ { "question" : "..", "answer" : ".." }], "categories" : ["..", ".."], "review_ids" : [12, 34, 56] }
Review{ "_id" : 12, "user_id" : 16, "place_id" : 1, "body" : “..”, "comments" : [ { "body" : "..", "user_id" : 1, "user_name" : ".." }, { "body" : "..", "user_id" : 5, "user_name" : ".."}], "ratings" : [ { "type" : "..", "value" : ".." }, { "type" : "..", "value" : ".." }]}
Place
Contacts
Ratings
Properties
Categories →
Category
RatingTypes
Questions
Answers
Review
AuthorSummary
Ratings
Comments
User
Reviews →
Соотношение запись/чтение
Жизненный цикл объекта
Так ли важна целостность данных?
To embed or not to embed?
Extra Stuff
GridFS
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
CarrierWave.configure do |config| config.storage = :grid_fs config.grid_fs_connection = Mongoid.database config.grid_fs_host = app_config['domain'] config.grid_fs_access_url = "/gridfs"end
class PhotoUploader < CarrierWave::Uploader::Base storage :grid_fsend
class Person include Mongoid::Document
field :first_name, :localize => true field :last_name, :localize => trueend
Локализация
{ "_id" : 1, "first_name" : { "ru" : "Иван" }, "last_name" : { "ru" : "Немытченко" } }
Полный фаршclass Person include Mongoid::Document include Mongoid::Timestamps include Mongoid::Versioning include Mongoid::Paranoia
field :first_name field :last_name key :first_name, :last_nameend
Гибридные приложения
https://github.com/a2ikm/activerecord-and-mongoid-sample
class TodoLog include Mongoid::Document field :todo_id, :type => Integer field :title, :type => String field :done, :type => Booleanend
class Todo < ActiveRecord::Base after_create :create_log after_update :create_log private def create_log TodoLog.create( :title => title, :done => !!done, :todo_id => id ) endend
Анти-mongoid
http://alexeypetrushin.github.com/mongodb_model/index.html
This tool exposes simplicity and power of
MongoDB and leverages its differences“
”Алексей Петрушин, автор MongoModel
Спасибо. Давайте поговорим.
@inem