Play With Mongoid!
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Play With Mongoid!

on

  • 8,040 views

 

Statistics

Views

Total Views
8,040
Views on SlideShare
6,544
Embed Views
1,496

Actions

Likes
19
Downloads
104
Comments
0

25 Embeds 1,496

http://blog.nosqlfan.com 516
http://slide.yoshiday.net 290
http://yukinishijima.net 239
http://blog.51osos.com 147
http://blog.yukinishijima.net 105
http://192.168.0.13 71
http://feed.feedsky.com 40
http://xianguo.com 29
https://twitter.com 7
http://reader.youdao.com 6
http://news.uplook.cn 6
http://webcache.googleusercontent.com 6
http://nuevospowerpoints.blogspot.com 5
http://us-w1.rockmelt.com 4
http://zhuaxia.com 4
http://www.hanrss.com 4
http://pinboard.in 3
http://www.zhuaxia.com 3
http://xue.uplook.cn 3
http://127.0.0.1 2
http://saoqiao.com 2
http://twitter.com 1
http://192.168.0.17 1
http://murray 1
http://wayback.archive.org 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Play With Mongoid! Presentation Transcript

  • 1. Play with Mongoid! @yuki24 5 MongoDB in Tokyo Mongo July, 30, 2011Saturday, July 30, 2011
  • 2. About me • Yuki Nishijima • @yuki24 on twitter and github • blog.yukinishijima.net • Kosen Conference • worked at • recently co-foundedSaturday, July 30, 2011
  • 3. Saturday, July 30, 2011
  • 4. Play with Mongoid!Saturday, July 30, 2011
  • 5. What is Mongoid? • Object-Document-Mapper (ODM) for MongoDB • written in Ruby • ActiveRecord-like APIsSaturday, July 30, 2011
  • 6. vs MongoMapper • Mongoid : • focuses on predictability • well-documented • MongoMapper : • sweet plugin system • has a larger communitySaturday, July 30, 2011
  • 7. Simple ExamplesSaturday, July 30, 2011
  • 8. Simple Examples class User include Mongoid::Document endSaturday, July 30, 2011
  • 9. Simple Examples class User include Mongoid::Document end > User => UserSaturday, July 30, 2011
  • 10. Simple Examples class User include Mongoid::Document end > User => User > user = User.new => #<User _id: 4e2bb6ee1d41c85118000004, _type: nil>Saturday, July 30, 2011
  • 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. Field Definition class User include Mongoid::Document field :first_name field :last_name endSaturday, July 30, 2011
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Available Types • Array • Hash • BigDecimal • Integer • Boolean • String • Date • Symbol • DateTime • Time • FloatSaturday, July 30, 2011
  • 24. Dynamic FieldsSaturday, July 30, 2011
  • 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. 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. 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. 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. 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. QueryingSaturday, July 30, 2011
  • 31. CriteriaSaturday, July 30, 2011
  • 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. Criteria evaluates lazilySaturday, July 30, 2011
  • 34. Criteria is chainableSaturday, July 30, 2011
  • 35. FindersSaturday, July 30, 2011
  • 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. Finders are NOT chainableSaturday, July 30, 2011
  • 38. Finders always touch the DBSaturday, July 30, 2011
  • 39. AssociationsSaturday, July 30, 2011
  • 40. Associations • has_many • has_one • has_and_belongs_to_many • belongs_to • embeds_many • embeds_one • embedded_inSaturday, July 30, 2011
  • 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. 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. 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. 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. Embedded Relations > user = User.new > user.cell_phones = [CellPhone.new(:name => "iPhone 5"), CellPhone.new(:name => "Nexus S")] > user.saveSaturday, July 30, 2011
  • 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. ValidationSaturday, July 30, 2011
  • 48. Mongoid includes ActiveModel::ValidationSaturday, July 30, 2011
  • 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. 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. Extra FeaturesSaturday, July 30, 2011
  • 52. Mongoid::Timestamps adds created_at and updated_at class User include Mongoid::Document include Mongoid::Timestamps endSaturday, July 30, 2011
  • 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. Mongoid:: Paranoia enables mongoid objects to soft-delete class User include Mongoid::Document include Mongoid::Paranoia endSaturday, July 30, 2011
  • 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. Mongoid::Versioning creates a version when saved class User include Mongoid::Document include Mongoid::Versioning endSaturday, July 30, 2011
  • 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. 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. 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. RSpec TestingSaturday, July 30, 2011
  • 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. 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. Let the hacking begin...Saturday, July 30, 2011
  • 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. Any Questions?Saturday, July 30, 2011
  • 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. We are hiring!Saturday, July 30, 2011
  • 68. Thank you!Saturday, July 30, 2011