Play with Mongoid!                                   @yuki24                            5   MongoDB            in Tokyo   ...
About me              •       Yuki Nishijima                    •     @yuki24 on twitter and github                    •  ...
Saturday, July 30, 2011
Play with Mongoid!Saturday, July 30, 2011
What is Mongoid?             • Object-Document-Mapper (ODM) for                     MongoDB             • written in Ruby ...
vs MongoMapper         • Mongoid :          • focuses on predictability          • well-documented         • MongoMapper :...
Simple ExamplesSaturday, July 30, 2011
Simple Examples        class User          include Mongoid::Document        endSaturday, July 30, 2011
Simple Examples        class User          include Mongoid::Document        end          > User          => UserSaturday, ...
Simple Examples        class User          include Mongoid::Document        end          > User          => User          ...
Simple Examples        class User          include Mongoid::Document        end          > User          => User          ...
Field Definition        class User          include Mongoid::Document          field :first_name          field :last_name ...
Field Definition        class User          include Mongoid::Document          field :first_name          field :last_name ...
Field Definition        class User          include Mongoid::Document          field :first_name          field :last_name ...
Field Definition        class User          include Mongoid::Document          field :first_name          field :last_name ...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Field Definition        class User          include Mongoid::Document          field :first_name, type: String          fie...
Available Types      • Array      • Hash      • BigDecimal • Integer      • Boolean    • String      • Date       • Symbol...
Dynamic FieldsSaturday, July 30, 2011
Dynamic Fields          > user = User.new          => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil,  ...
Dynamic Fields          > user = User.new          => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil,  ...
Dynamic Fields          > user = User.new          => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil,  ...
Dynamic Fields          > user = User.new          => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil,  ...
Dynamic Fields          > user = User.new          => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil,  ...
QueryingSaturday, July 30, 2011
CriteriaSaturday, July 30, 2011
Criteria                •         Model.all_in    •   Model.limit                •         Model.also_in   •   Model.near ...
Criteria evaluates lazilySaturday, July 30, 2011
Criteria is chainableSaturday, July 30, 2011
FindersSaturday, July 30, 2011
Finders                •         Model.all                •         Model.count                •         Model.exists?    ...
Finders are NOT chainableSaturday, July 30, 2011
Finders always                          touch the DBSaturday, July 30, 2011
AssociationsSaturday, July 30, 2011
Associations                  •       has_many                  •       has_one                  •       has_and_belongs_t...
Referenced Relations        class User          include Mongoid::Document          has_many :laptops        end        cla...
Referenced Relations        > user = User.new        > user.laptops =            [Laptop.new(:name => "MacBook Air"), Lapt...
Referenced Relations        > user = User.new        > user.laptops =            [Laptop.new(:name => "MacBook Air"), Lapt...
Embedded Relations        class User          include Mongoid::Document          embeds_many :cell_phones        end      ...
Embedded Relations        > user = User.new        > user.cell_phones =            [CellPhone.new(:name => "iPhone 5"), Ce...
Embedded Relations        > user = User.new        > user.cell_phones =            [CellPhone.new(:name => "iPhone 5"), Ce...
ValidationSaturday, July 30, 2011
Mongoid includes                   ActiveModel::ValidationSaturday, July 30, 2011
Validations                •         validates_acceptance_of                •         validates_associated                ...
Validations                •         validates_acceptance_of                •         validates_associated                ...
Extra FeaturesSaturday, July 30, 2011
Mongoid::Timestamps        adds created_at and updated_at                class User                  include Mongoid::Docu...
Mongoid::Timestamps        adds created_at and updated_at                class User                  include Mongoid::Docu...
Mongoid:: Paranoia        enables mongoid objects to soft-delete                class User                  include Mongoi...
Mongoid:: Paranoia        enables mongoid objects to soft-delete                class User                  include Mongoi...
Mongoid::Versioning        creates a version when saved                class User                  include Mongoid::Docume...
Mongoid::Versioning        creates a version when saved               > user = User.first               => #<User _id: 4e3...
Mongoid::Versioning        creates a version when saved               > user = User.first               => #<User _id: 4e3...
Mongoid::Versioning        creates a version when saved               > user = User.first               => #<User _id: 4e3...
RSpec TestingSaturday, July 30, 2011
RSpec Testing                  gem "bson_ext", ">= 1.3.1"                  gem "mongoid", ">= 2.0.2"                  grou...
RSpec Testing                  RSpec.configure do |config|                    # add matchers for Mongoid                  ...
Let the hacking begin...Saturday, July 30, 2011
More Information?    •       Official page : http://mongoid.org/    •       Mailing list : https://groups.google.com/forum/...
Any Questions?Saturday, July 30, 2011
Startup Weekend!                          No Talk, All Action.             • Kyoto : August, 5 - 7, at Kyoto Research Park...
We are hiring!Saturday, July 30, 2011
Thank you!Saturday, July 30, 2011
Upcoming SlideShare
Loading in...5
×

Play With Mongoid!

7,510

Published on

0 Comments
19 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,510
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
104
Comments
0
Likes
19
Embeds 0
No embeds

No notes for slide

Transcript of "Play With Mongoid!"

  1. 1. Play with Mongoid! @yuki24 5 MongoDB in Tokyo Mongo July, 30, 2011Saturday, July 30, 2011
  2. 2. About me • Yuki Nishijima • @yuki24 on twitter and github • blog.yukinishijima.net • Kosen Conference • worked at • recently co-foundedSaturday, July 30, 2011
  3. 3. Saturday, July 30, 2011
  4. 4. Play with Mongoid!Saturday, July 30, 2011
  5. 5. What is Mongoid? • Object-Document-Mapper (ODM) for MongoDB • written in Ruby • ActiveRecord-like APIsSaturday, July 30, 2011
  6. 6. vs MongoMapper • Mongoid : • focuses on predictability • well-documented • MongoMapper : • sweet plugin system • has a larger communitySaturday, July 30, 2011
  7. 7. Simple ExamplesSaturday, July 30, 2011
  8. 8. Simple Examples class User include Mongoid::Document endSaturday, July 30, 2011
  9. 9. Simple Examples class User include Mongoid::Document end > User => UserSaturday, July 30, 2011
  10. 10. Simple Examples class User include Mongoid::Document end > User => User > user = User.new => #<User _id: 4e2bb6ee1d41c85118000004, _type: nil>Saturday, July 30, 2011
  11. 11. Simple Examples class User include Mongoid::Document end > User => User > user = User.new => #<User _id: 4e2bb6ee1d41c85118000004, _type: nil> > user.id => BSON::ObjectId(4e2bb6ee1d41c85118000004)Saturday, July 30, 2011
  12. 12. Field Definition class User include Mongoid::Document field :first_name field :last_name endSaturday, July 30, 2011
  13. 13. Field Definition class User include Mongoid::Document field :first_name field :last_name end > user = User.new :first_name => "Yuki", :last_name => "Nishijima"Saturday, July 30, 2011
  14. 14. Field Definition class User include Mongoid::Document field :first_name field :last_name end > user = User.new :first_name => "Yuki", :last_name => "Nishijima" > user.first_name => "Yuki"Saturday, July 30, 2011
  15. 15. Field Definition class User include Mongoid::Document field :first_name field :last_name end > user = User.new :first_name => "Yuki", :last_name => "Nishijima" > user.first_name => "Yuki" > user.last_name => "Nishijima"Saturday, July 30, 2011
  16. 16. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String end > user = User.new :first_name => "Yuki", :last_name => "Nishijima" > user.first_name => "Yuki" > user.last_name => "Nishijima"Saturday, July 30, 2011
  17. 17. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String end > user = User.new :first_name => 123, :last_name => trueSaturday, July 30, 2011
  18. 18. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String end > user = User.new :first_name => 123, :last_name => true > user.first_name => "123" > user.last_name => "true"Saturday, July 30, 2011
  19. 19. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String field :location, type: String, default: ”Tokyo, Japan” endSaturday, July 30, 2011
  20. 20. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String field :location, type: String, default: "Tokyo, Japan" end > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan">Saturday, July 30, 2011
  21. 21. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String field :location, type: String, default: ”Tokyo, Japan” end > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan"> > user.first_name => nilSaturday, July 30, 2011
  22. 22. Field Definition class User include Mongoid::Document field :first_name, type: String field :last_name, type: String field :location, type: String, default: ”Tokyo, Japan” end > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan"> > user.first_name => nil > user.location => "Tokyo, Japan"Saturday, July 30, 2011
  23. 23. Available Types • Array • Hash • BigDecimal • Integer • Boolean • String • Date • Symbol • DateTime • Time • FloatSaturday, July 30, 2011
  24. 24. Dynamic FieldsSaturday, July 30, 2011
  25. 25. Dynamic Fields > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan">Saturday, July 30, 2011
  26. 26. Dynamic Fields > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan"> > user.hometown => NoMethodErrorSaturday, July 30, 2011
  27. 27. Dynamic Fields > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan"> > user.hometown => NoMethodError > user.write_attribute(:hometown, "Chiba, Japan") => "Chiba, Japan"Saturday, July 30, 2011
  28. 28. Dynamic Fields > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan"> > user.hometown => NoMethodError > user.write_attribute(:hometown, "Chiba, Japan") => "Chiba, Japan" > user.hometown => "Tokyo, Japan"Saturday, July 30, 2011
  29. 29. Dynamic Fields > user = User.new => #<User _id: 4e2bca2e1d41c85118000014, _type: nil, first_name: nil, last_name: nil, location: "Tokyo, Japan"> > user.hometown => NoMethodError > user.write_attribute(:hometown, "Chiba, Japan") => "Chiba, Japan" > user.hometown => "Tokyo, Japan" > user.update_attribute(:hometown, "Chiba, Japan") => trueSaturday, July 30, 2011
  30. 30. QueryingSaturday, July 30, 2011
  31. 31. CriteriaSaturday, July 30, 2011
  32. 32. Criteria • Model.all_in • Model.limit • Model.also_in • Model.near • Criteria#and • Model.not_in • Model.any_in • Model.only • Model.any_of • Model.order_by • Model.asc • Model.skip • Model.desc • Model.where • Model.exclude • Model.withoutSaturday, July 30, 2011
  33. 33. Criteria evaluates lazilySaturday, July 30, 2011
  34. 34. Criteria is chainableSaturday, July 30, 2011
  35. 35. FindersSaturday, July 30, 2011
  36. 36. Finders • Model.all • Model.count • Model.exists? • Model.find • Model.find_or_create_by • Model.find_or_initialize_by • Model.first • Model.lastSaturday, July 30, 2011
  37. 37. Finders are NOT chainableSaturday, July 30, 2011
  38. 38. Finders always touch the DBSaturday, July 30, 2011
  39. 39. AssociationsSaturday, July 30, 2011
  40. 40. Associations • has_many • has_one • has_and_belongs_to_many • belongs_to • embeds_many • embeds_one • embedded_inSaturday, July 30, 2011
  41. 41. Referenced Relations class User include Mongoid::Document has_many :laptops end class Laptop include Mongoid::Document field :name, type: String belongs_to :user endSaturday, July 30, 2011
  42. 42. Referenced Relations > user = User.new > user.laptops = [Laptop.new(:name => "MacBook Air"), Laptop.new(:name => "MacBook Pro")] > user.save > user => #<User _id: 4e3380db1d41c804d8000001, _type: nil>Saturday, July 30, 2011
  43. 43. Referenced Relations > user = User.new > user.laptops = [Laptop.new(:name => "MacBook Air"), Laptop.new(:name => "MacBook Pro")] > user.save > user => #<User _id: 4e3380db1d41c804d8000001, _type: nil> > user.laptops => [#<Laptop _id: 4e3381051d41c804d8000002, _type: nil, name: "MacBook Air", user_id: BSON::ObjectId(4e3380db1d41c804d8000001)>, #<Laptop _id: 4e3381051d41c804d8000003, _type: nil, name: "MacBook Pro", user_id: BSON::ObjectId(4e3380db1d41c804d8000001)>]Saturday, July 30, 2011
  44. 44. Embedded Relations class User include Mongoid::Document embeds_many :cell_phones end class CellPhone include Mongoid::Document field :name, type: String embedded_in :user endSaturday, July 30, 2011
  45. 45. Embedded Relations > user = User.new > user.cell_phones = [CellPhone.new(:name => "iPhone 5"), CellPhone.new(:name => "Nexus S")] > user.saveSaturday, July 30, 2011
  46. 46. Embedded Relations > user = User.new > user.cell_phones = [CellPhone.new(:name => "iPhone 5"), CellPhone.new(:name => "Nexus S")] > user.save { "_id" : ObjectId("4e3316e51d41c87a4a000001"), "cell_phones" : [ { "name" : "iPhone 5", "_id" : ObjectId("4e3317061d41c87a4a000002") }, { "name" : "Nexus S", "_id" : ObjectId("4e3317061d41c87a4a000003") } ] }Saturday, July 30, 2011
  47. 47. ValidationSaturday, July 30, 2011
  48. 48. Mongoid includes ActiveModel::ValidationSaturday, July 30, 2011
  49. 49. Validations • 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_ofSaturday, July 30, 2011
  50. 50. Validations • validates_acceptance_of • validates_associated • validates_confirmation_of • validates_exclusion_of • validates_format_of • validates_inclusion_of for embedded documents, • validates_length_of it only checks the field is unique within the context • validates_numericality_of of the parent document. • validates_presence_of • validates_uniqueness_ofSaturday, July 30, 2011
  51. 51. Extra FeaturesSaturday, July 30, 2011
  52. 52. Mongoid::Timestamps adds created_at and updated_at class User include Mongoid::Document include Mongoid::Timestamps endSaturday, July 30, 2011
  53. 53. Mongoid::Timestamps adds created_at and updated_at class User include Mongoid::Document include Mongoid::Timestamps end class User include Mongoid::Document include Mongoid::Timestamps::Created end class User include Mongoid::Document include Mongoid::Timestamps::Updated endSaturday, July 30, 2011
  54. 54. Mongoid:: Paranoia enables mongoid objects to soft-delete class User include Mongoid::Document include Mongoid::Paranoia endSaturday, July 30, 2011
  55. 55. Mongoid:: Paranoia enables mongoid objects to soft-delete class User include Mongoid::Document include Mongoid::Paranoia end # Sets the deleted_at field to the current time. person.delete # Permanently deletes the document. person.delete! # Permanently delete the document with callbacks. person.destroy! # Brings the "deleted" document back to life. person.restoreSaturday, July 30, 2011
  56. 56. Mongoid::Versioning creates a version when saved class User include Mongoid::Document include Mongoid::Versioning endSaturday, July 30, 2011
  57. 57. Mongoid::Versioning creates a version when saved > user = User.first => #<User _id: 4e31c1bc1d41c85e5f000001, _type: nil, version: 1, first_name: nil, last_name: nil>Saturday, July 30, 2011
  58. 58. Mongoid::Versioning creates a version when saved > user = User.first => #<User _id: 4e31c1bc1d41c85e5f000001, _type: nil, version: 1, first_name: nil, last_name: nil> > user.first_name = "Yuki" > user.save > user.versions => [#<User _id: 4e330e3a1d41c878f1000001, _type: nil, version: 1, first_name: nil, last_name: nil>]Saturday, July 30, 2011
  59. 59. Mongoid::Versioning creates a version when saved > user = User.first => #<User _id: 4e31c1bc1d41c85e5f000001, _type: nil, version: 1, first_name: nil, last_name: nil> > user.first_name = "Yuki" > user.save > user.versions => [#<User _id: 4e330e3a1d41c878f1000001, _type: nil, version: 1, first_name: nil, last_name: nil>] > user.last_name = "Nishijima" > user.save > user.versions => [#<User _id: 4e330e3a1d41c878f1000001, _type: nil, version: 1, first_name: nil, last_name: nil>, #<User _id: 4e330e4c1d41c878f1000002, _type: nil, version: 2, first_name: "Yuki", last_name: nil>]Saturday, July 30, 2011
  60. 60. RSpec TestingSaturday, July 30, 2011
  61. 61. RSpec Testing gem "bson_ext", ">= 1.3.1" gem "mongoid", ">= 2.0.2" group :development, :test do gem "rspec-rails", ">= 2.6.1” end group :test do gem "database_cleaner", ">= 0.6.7" gem "mongoid-rspec", ">= 1.4.4" gem "factory_girl_rails", ">= 1.1.rc1" gem "cucumber-rails", ">= 1.0.2" endSaturday, July 30, 2011
  62. 62. RSpec Testing RSpec.configure do |config|   # add matchers for Mongoid config.include Mongoid::Matchers   # Clean up the database   require database_cleaner   config.before(:suite) do     DatabaseCleaner.strategy = :truncation     DatabaseCleaner.orm = "mongoid"   end   config.before(:each) do     DatabaseCleaner.clean   end endSaturday, July 30, 2011
  63. 63. Let the hacking begin...Saturday, July 30, 2011
  64. 64. More Information? • Official page : http://mongoid.org/ • Mailing list : https://groups.google.com/forum/#!forum/mongoid • github : https://github.com/mongoid • Durran Jordan(Author) : https://twitter.com/#!/modetojoySaturday, July 30, 2011
  65. 65. Any Questions?Saturday, July 30, 2011
  66. 66. Startup Weekend! No Talk, All Action. • Kyoto : August, 5 - 7, at Kyoto Research Park • Tokyo : August, 19 - 21, at mixi • Fukuoka : August 26 - 28, at Kyushu UniversitySaturday, July 30, 2011
  67. 67. We are hiring!Saturday, July 30, 2011
  68. 68. Thank you!Saturday, July 30, 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×