Your SlideShare is downloading. ×
Mongoid in the real world
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Mongoid in the real world

5,446
views

Published on


1 Comment
10 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,446
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
73
Comments
1
Likes
10
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Mongoid in theReal WorldToronto Ruby Brigade Kevin FaustinoDecember 14, 2010 @kfaustino http://adventuresincoding.com
  • 2. Thanks to our sponsors
  • 3. What is Mongoid?
  • 4. Object Document Mapper
  • 5. Using MongoidEveryday Mongoid Integrations
  • 6. class Postend
  • 7. class Post include Mongoid::Documentend
  • 8. Types
  • 9. class Post include Mongoid::Document include Mongoid::Timestamps field :title field :content field :published_on, :type => DateTime field :slugend
  • 10. Valid TypesArray, BigDecimal, Boolean, Date, DateTime, Float, Hash,Integer, String, Symbol, Time
  • 11. Associations
  • 12. class Person include Mongoid::Document field :first_name field :last_name embeds_one :addressendclass Address include Mongoid::Document field :street field :city embedded_in :person, :inverse_of => :addressend
  • 13. { "_id" : ObjectId("4d06eaa46c50a1031a000001"), "address" : { "_id" : ObjectId("4d06eade6c50a1031a000002"), "street" : "123 Liberty St.", "city" : "Toronto" }, "first_name" : "Kevin", "last_name" : "Faustino"}
  • 14. class Blog include Mongoid::Document references_many :postsendclass Post include Mongoid::Document referenced_in :blogend
  • 15. { "_id" : ObjectId("4c9f54866c50a10a75000003"), "blog_id" : ObjectId("4ca531c46c50a12d1b000002"), "content" : "What is Faraday? ...", "featured_image_filename" : "faraday.jpg", "slug" : "building-modular-http-client-code-with-faraday", "title" : "Building modular HTTP client code withFaraday",}
  • 16. Validations
  • 17. ActiveModel Validators + More validates_acceptance_of validates_associated validates_confirmation_of validates_exclusion_of validates_format_of validates_inclusion_of validates_length_of validates_numericality_of validates_presence_of validates_uniqueness_of
  • 18. Indexing
  • 19. Defining Indexes# uniqueindex :ssn, :unique => true# Compound indexesindex([ [ :first_name, Mongo::ASCENDING ], [ :last_name, Mongo::ASCENDING ]])# Foreign Key indexesreferenced_in :post, :inverse_of=> :comments, :index => true
  • 20. Using MongoidEveryday Mongoid Integrations
  • 21. Document Limit
  • 22. 4 MB
  • 23. Testing
  • 24. RSpec Integration# in spec/spec_helper.rbRSpec.configure do |config| config.mock_with :rspec config.before :each do Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop) endend
  • 25. Shoulda Matchers?
  • 26. Use Remarkable# in spec/spec_helper.rbrequire remarkable/active_modelrequire remarkable/mongoid
  • 27. Mongoid Matchershave_fieldreference_onereference_manybe_referenced_inembed_oneembed_manybe_embedded_invalidate_uniqueness_ofvalidate_associationAll ActiveModel validations from Remarkable::ActiveModel
  • 28. Cucumber Integration# features/support/hooks.rbBefore do |scenario| Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)end
  • 29. Factory Girl
  • 30. Works out of the boxFactoryGirl.define do factory :post do title My first blog post author Kevin Faustino content Hello World endend
  • 31. Factories with Cucumber# features/support/env.rbrequire factory_girlrequire factory_girl/step_definitions
  • 32. Monkey Patched Step Definition# features/step_definitions/mongoid_steps.rbGiven /^an? (.+) exists with an? (.+) of "([^"]*)"$/do |model, field, value| factory_name = model.gsub( , _) Factory factory_name, field => valueend
  • 33. Example Feature UsageGiven the following posts exist: | title | excerpt | | 10 greatest Batman Villians | We countdown ... | | The Death of Batman | Batman falls ... |
  • 34. Caching in Rails
  • 35. Create a Cache-Keymodule Cacheable def cache_key if new_record? "#{collection_name}/#{id}/new" elsif respond_to?(:updated_at) "#{collection_name}/#{id}-#{updated_at.to_i}" else "#{collection_name}/#{id}" end endend
  • 36. Safe Operations
  • 37. post = Post.safely(:w => 2).create( :title => We will receive verification)post.safely(:w => 2).destroy
  • 38. Safely Options:w - A client can block until a writeoperation has been replicated to N servers:fsync - force the database to fsync all filesbefore returning
  • 39. Enslave
  • 40. Query from your Slaves# Query specificPost.where(:title => Toronto Ruby).enslave# Document specificclass Post include Mongoid::Document enslaveend
  • 41. Dropping Down
  • 42. When you need moreperformance, drop down to the driver
  • 43. collection = mongo-ruby-driver# Add a post id only if it does not already exist on# the setcollection.update({_id => id}, {$addToSet =>{post_ids => post.id}})
  • 44. Using MongoidEveryday Mongoid Integrations
  • 45. Full Text-Search
  • 46. Use Solr@solr = RSolr.connect :url => "http://127.0.0.1:8983/solr"@solr.add( { :id => post.id, :text => post.content, :published_on => post.published_on.strftime("%Y-%m-%dT%H:%M:00Z") }, { :add_attributes => { :commitWithin => 10000 } })
  • 47. Photo Uploads
  • 48. Not with paperclip! X
  • 49. Agnostic
  • 50. Document Exampleclass Post include Mongoid::Document include Mongoid::Timestamps field :title field :author field :content field :slug referenced_in :blog mount_uploader :featured_image, ImageUploaderend
  • 51. Authentication
  • 52. Use Deviseclass User include Mongoid::Document devise :database_authenticatable, :rememberable, :trackable, :validatableend
  • 53. Coming Soon
  • 54. Many to Many supportwithout dropping down to the driver
  • 55. Thank you!