Successfully reported this slideshow.

MongoDB on Rails (and Ruby)

2

Share

Upcoming SlideShare
Mongodb railscamphh
Mongodb railscamphh
Loading in …3
×
1 of 213
1 of 213

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

MongoDB on Rails (and Ruby)

  1. 1. on ruby and r ails mongo berlin 2010 jan krutisch <jan@krutisch.de> http://jan.krutisch.de/ Donnerstag, 7. Oktober 2010
  2. 2. I Donnerstag, 7. Oktober 2010
  3. 3. Web Developer I Donnerstag, 7. Oktober 2010
  4. 4. Web Developer I Donnerstag, 7. Oktober 2010
  5. 5. Web Developer github.com/halfbyte I Donnerstag, 7. Oktober 2010
  6. 6. Web Developer github.com/halfbyte I jan.krutisch.de Donnerstag, 7. Oktober 2010
  7. 7. Web Developer github.com/halfbyte Tinkerer I jan.krutisch.de Donnerstag, 7. Oktober 2010
  8. 8. Web Developer github.com/halfbyte Tinkerer I jan.krutisch.de twitter.com/halfbyte Donnerstag, 7. Oktober 2010
  9. 9. ruby Donnerstag, 7. Oktober 2010
  10. 10. Find examples here: http://github.com/halfbyte/mongo_ruby_examples Donnerstag, 7. Oktober 2010
  11. 11. Basic driver usage Donnerstag, 7. Oktober 2010
  12. 12. gem install mongo bson_ext Donnerstag, 7. Oktober 2010
  13. 13. init Donnerstag, 7. Oktober 2010
  14. 14. getting connections Donnerstag, 7. Oktober 2010
  15. 15. @connection = Mongo::Connection.new Donnerstag, 7. Oktober 2010
  16. 16. @connection = Mongo::Connection.new( 'localhost', 27017, :pool_size => 5, :timeout => 20 ) Donnerstag, 7. Oktober 2010
  17. 17. @connection = Mongo::Connection.from_uri( "mongodb://localhost:27017/test" ) Donnerstag, 7. Oktober 2010
  18. 18. Choose a database. Donnerstag, 7. Oktober 2010
  19. 19. @connection.database_names #=> ["admin", "local"] Donnerstag, 7. Oktober 2010
  20. 20. @db = @connection['test'] Donnerstag, 7. Oktober 2010
  21. 21. @db = @connection.db('test') Donnerstag, 7. Oktober 2010
  22. 22. collections Donnerstag, 7. Oktober 2010
  23. 23. @collection = @db['books'] Donnerstag, 7. Oktober 2010
  24. 24. @collection = @db.collection('books') Donnerstag, 7. Oktober 2010
  25. 25. subcollections Donnerstag, 7. Oktober 2010
  26. 26. @collection = @db['books.reviews'] Donnerstag, 7. Oktober 2010
  27. 27. @collection = @db['books']['reviews'] Donnerstag, 7. Oktober 2010
  28. 28. CRUD Donnerstag, 7. Oktober 2010
  29. 29. Create Read Update Delete Donnerstag, 7. Oktober 2010
  30. 30. Create Read Update Delete Donnerstag, 7. Oktober 2010
  31. 31. insert Donnerstag, 7. Oktober 2010
  32. 32. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } @db['quotes'].insert(doc) Donnerstag, 7. Oktober 2010
  33. 33. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } @db['quotes'].save(doc) Donnerstag, 7. Oktober 2010
  34. 34. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } @db['quotes'].save(doc) Donnerstag, 7. Oktober 2010
  35. 35. safe (using getlasterror) Donnerstag, 7. Oktober 2010
  36. 36. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } @db['quotes'].save(doc, :safe => true) Donnerstag, 7. Oktober 2010
  37. 37. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } @db['quotes'].save(doc, :safe => true) Donnerstag, 7. Oktober 2010
  38. 38. how about getting back an id? Donnerstag, 7. Oktober 2010
  39. 39. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } id = @db['quotes'].save(doc) Donnerstag, 7. Oktober 2010
  40. 40. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } id = @db['quotes'].save(doc) Donnerstag, 7. Oktober 2010
  41. 41. Create Read Update Delete Donnerstag, 7. Oktober 2010
  42. 42. Create Read Update Delete Donnerstag, 7. Oktober 2010
  43. 43. Create Read Update Delete Donnerstag, 7. Oktober 2010
  44. 44. upsert Donnerstag, 7. Oktober 2010
  45. 45. doc = @db['quotes'].find_one(id) doc[:from] = "Yogi Berra, famous baseball player" @db['quotes'].save(doc) Donnerstag, 7. Oktober 2010
  46. 46. Create Read Update Delete Donnerstag, 7. Oktober 2010
  47. 47. Create Read Update Delete Donnerstag, 7. Oktober 2010
  48. 48. @db['quotes'].update( {:_id => doc['_id']}, { :from => "Yogi Berra", :text => "You can observe a lot just by watching.", :tags => ['baseball', 'wit'] } ) Donnerstag, 7. Oktober 2010
  49. 49. atomic updates Donnerstag, 7. Oktober 2010
  50. 50. @db['quotes'].update( {"from" => "Yogi Berra"}, {"$inc" => {"reads" => 1 } } ) Donnerstag, 7. Oktober 2010
  51. 51. @db['quotes'].update( {"from" => "Yogi Berra"}, {"$inc" => {"reads" => 1 } } ) Donnerstag, 7. Oktober 2010
  52. 52. $inc $addToSet $set $pop $unset $pull $push $pullAll $pushAll $ Donnerstag, 7. Oktober 2010
  53. 53. @db['people'].update( {"tags" => "cool"}, "$addToSet" => {"tags" => 'froody'}, :multi => true ) Donnerstag, 7. Oktober 2010
  54. 54. @db['people'].update( {"tags" => "cool"}, "$addToSet" => {"tags" => 'froody'}, :multi => true ) Donnerstag, 7. Oktober 2010
  55. 55. @db['people'].update( {"tags" => "cool"}, { "$addToSet" => { "tags" => { "$each" => ['froody', 'hoopy'] } } }, :safe => true, :multi => true ) Donnerstag, 7. Oktober 2010
  56. 56. @db['people'].update( {"tags" => "cool"}, { "$addToSet" => { "tags" => { "$each" => ['froody', 'hoopy'] } } }, :safe => true, :multi => true ) Donnerstag, 7. Oktober 2010
  57. 57. @db['people'].update( {"tags" => "cool"}, {"$set" => {"tags.$" => "fresh"}}, :safe => true, :multi => true ) Donnerstag, 7. Oktober 2010
  58. 58. @db['people'].update( {"tags" => "cool"}, {"$set" => {"tags.$" => "fresh"}}, :safe => true, :multi => true ) Donnerstag, 7. Oktober 2010
  59. 59. Create Read Update Delete Donnerstag, 7. Oktober 2010
  60. 60. Create Read Update Delete Donnerstag, 7. Oktober 2010
  61. 61. @db['people'].remove Donnerstag, 7. Oktober 2010
  62. 62. @db['numbers'].remove( {"$lt" => 30}, :safe => true ) Donnerstag, 7. Oktober 2010
  63. 63. Create Read Update Delete Donnerstag, 7. Oktober 2010
  64. 64. Create Read Update Delete Donnerstag, 7. Oktober 2010
  65. 65. all Donnerstag, 7. Oktober 2010
  66. 66. @db['quotes'].find.each do |row| puts row.inspect end Donnerstag, 7. Oktober 2010
  67. 67. one Donnerstag, 7. Oktober 2010
  68. 68. row = @db['quotes'].find_one Donnerstag, 7. Oktober 2010
  69. 69. exact query Donnerstag, 7. Oktober 2010
  70. 70. @db['quotes'].find(:from => "Yogi Berra") Donnerstag, 7. Oktober 2010
  71. 71. more queries Donnerstag, 7. Oktober 2010
  72. 72. 100.times do |i| db['numbers'].insert({"i" => i}) end Donnerstag, 7. Oktober 2010
  73. 73. db['numbers'].find("i" => {"$lt" => 2}) Donnerstag, 7. Oktober 2010
  74. 74. $lt < $gt > $lte <= $gte >= $ne != Donnerstag, 7. Oktober 2010
  75. 75. @db['people'].find( :tags => { "$in" => ['cool', 'weird'] } ) Donnerstag, 7. Oktober 2010
  76. 76. obj = { "_id"=>BSON::ObjectID('4c706af16261040680000369'), "name"=>"Vernon Kreiger", "address"=>{ "street"=>"536 Haleigh Locks", "city"=>"Port Kiannahaven", "zip"=>"80730-0214", "country"=>"Fakistan" }, "tags"=>["cool", "weird"] } Donnerstag, 7. Oktober 2010
  77. 77. obj = { "_id"=>BSON::ObjectID('4c706af16261040680000369'), "name"=>"Vernon Kreiger", "address"=>{ "street"=>"536 Haleigh Locks", "city"=>"Port Kiannahaven", "zip"=>"80730-0214", "country"=>"Fakistan" }, "tags"=>["cool", "weird"] } Donnerstag, 7. Oktober 2010
  78. 78. $in IN (2,3,4) $nin NOT IN $all [2,3] ~ [1,2,3] Donnerstag, 7. Oktober 2010
  79. 79. $mod yah, RLY $size okay $exists NOT NULL $type huh? Donnerstag, 7. Oktober 2010
  80. 80. Let‘s go deep. Donnerstag, 7. Oktober 2010
  81. 81. @db['people'].find("address.city" => "Berlin") Donnerstag, 7. Oktober 2010
  82. 82. @db['people'].find("address.city" => "Berlin") Donnerstag, 7. Oktober 2010
  83. 83. /stand back/ Donnerstag, 7. Oktober 2010
  84. 84. @db['people'].find("address.city" => /haven/) Donnerstag, 7. Oktober 2010
  85. 85. @db['people'].find("address.city" => /haven/) Donnerstag, 7. Oktober 2010
  86. 86. I‘m in ur database, executin ur javascript Donnerstag, 7. Oktober 2010
  87. 87. @db['numbers'].find("$where" => "this.i < 2") Donnerstag, 7. Oktober 2010
  88. 88. boolsh Donnerstag, 7. Oktober 2010
  89. 89. not Donnerstag, 7. Oktober 2010
  90. 90. @db['numbers'].find( "i" => { "$not" => {"$lt" => 97} } ) Donnerstag, 7. Oktober 2010
  91. 91. and Donnerstag, 7. Oktober 2010
  92. 92. @db['numbers'].find( "i" => { "$lt" => 52, "$gt" => 48 } ) Donnerstag, 7. Oktober 2010
  93. 93. or Donnerstag, 7. Oktober 2010
  94. 94. @db['numbers'].find( "$or" => [ { "i" => { "$lt" => 2 } }, { "i" => { "$gt" => 97 } } ] ) Donnerstag, 7. Oktober 2010
  95. 95. Sorting Donnerstag, 7. Oktober 2010
  96. 96. @db['people'].find().sort("address.street") Donnerstag, 7. Oktober 2010
  97. 97. @db['people'].find().sort("address.street") Donnerstag, 7. Oktober 2010
  98. 98. @db['people'].find().sort("address.street") Donnerstag, 7. Oktober 2010
  99. 99. @db['people'].find().sort("address.street") Donnerstag, 7. Oktober 2010
  100. 100. @db['people'].find().sort("address.street", :asc) Donnerstag, 7. Oktober 2010
  101. 101. @db['people'].find().sort("address.street", :asc) Donnerstag, 7. Oktober 2010
  102. 102. @db['people'].find().sort( "address.street", Mongo::ASCENDING ) Donnerstag, 7. Oktober 2010
  103. 103. @db['people'].find().sort( "address.street", Mongo::ASCENDING ) Donnerstag, 7. Oktober 2010
  104. 104. Pagination Donnerstag, 7. Oktober 2010
  105. 105. @db['numbers'].find.sort("i").limit(10) Donnerstag, 7. Oktober 2010
  106. 106. @db['numbers'].find.sort("i").limit(10).skip(50) Donnerstag, 7. Oktober 2010
  107. 107. Aggregation Donnerstag, 7. Oktober 2010
  108. 108. Countin‘ Bizness Donnerstag, 7. Oktober 2010
  109. 109. @db['numbers'].find.count Donnerstag, 7. Oktober 2010
  110. 110. Distinct Donnerstag, 7. Oktober 2010
  111. 111. @db['people'].distinct('tags') Donnerstag, 7. Oktober 2010
  112. 112. Group Donnerstag, 7. Oktober 2010
  113. 113. Poor mans map/reduce Donnerstag, 7. Oktober 2010
  114. 114. @db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize ) Donnerstag, 7. Oktober 2010
  115. 115. @db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize ) Donnerstag, 7. Oktober 2010
  116. 116. @db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize ) Donnerstag, 7. Oktober 2010
  117. 117. @db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize ) Donnerstag, 7. Oktober 2010
  118. 118. @db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize ) Donnerstag, 7. Oktober 2010
  119. 119. function(doc, prev) { for(i in doc.tags) { if (doc.tags[i] in prev.tags) { prev.tags[doc.tags[i]]++ } else { prev.tags[doc.tags[i]] =1 } } } Donnerstag, 7. Oktober 2010
  120. 120. {"created_at"=>2010-09-19 22:00:00 UTC, "tags"=>{"foo"=>11.0, "dumb"=>12.0, "stupid"=>7.0, "bar"=>7.0, "cool"=>14.0, "weird"=>17.0}} {"created_at"=>2010-09-20 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "stupid"=>5.0, "foo"=>10.0, "cool"=>8.0, "weird"=>9.0, "bar"=>15.0}} {"created_at"=>2010-09-22 22:00:00 UTC, "tags"=>{"weird"=>15.0, "bar"=>9.0, "stupid"=>17.0, "cool"=>11.0, "dumb"=>10.0, "foo"=>12.0}} {"created_at"=>2010-09-15 22:00:00 UTC, "tags"=>{"foo"=>11.0, "weird"=>7.0, "stupid"=>10.0, "cool"=>11.0, "bar"=>5.0, "dumb"=>8.0}} {"created_at"=>2010-09-25 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "weird"=>14.0, "cool"=>8.0, "foo"=>21.0, "bar"=>11.0, "stupid"=>13.0}} {"created_at"=>2010-09-28 22:00:00 UTC, "tags"=>{"cool"=>11.0, "dumb"=>16.0, "stupid"=>11.0, "weird"=>15.0, "foo"=>9.0, "bar"=>16.0}} {"created_at"=>2010-09-10 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>9.0, "bar"=>8.0, "cool"=>14.0, "stupid"=>11.0, "foo"=>11.0}} {"created_at"=>2010-09-03 22:00:00 UTC, "tags"=>{"weird"=>14.0, "dumb"=>15.0, "stupid"=>11.0, "foo"=>16.0, "bar"=>20.0, "cool"=>10.0}} {"created_at"=>2010-09-21 22:00:00 UTC, "tags"=>{"weird"=>15.0, "cool"=>14.0, "foo"=>13.0, "stupid"=>6.0, "bar"=>11.0, "dumb"=>9.0}} {"created_at"=>2010-09-23 22:00:00 UTC, "tags"=>{"weird"=>15.0, "stupid"=>15.0, "dumb"=>15.0, "foo"=>16.0, "cool"=>10.0, "bar"=>11.0}} {"created_at"=>2010-09-29 22:00:00 UTC, "tags"=>{"bar"=>9.0, "cool"=>14.0, "weird"=>16.0, "foo"=>8.0, "dumb"=>9.0, "stupid"=>12.0}} {"created_at"=>2010-09-27 22:00:00 UTC, "tags"=>{"cool"=>13.0, "dumb"=>10.0, "stupid"=>12.0, "bar"=>8.0, "foo"=>16.0, "weird"=>13.0}} {"created_at"=>2010-09-04 22:00:00 UTC, "tags"=>{"cool"=>11.0, "bar"=>9.0, "stupid"=>6.0, "weird"=>11.0, "dumb"=>8.0, "foo"=>11.0}} {"created_at"=>2010-09-08 22:00:00 UTC, "tags"=>{"stupid"=>12.0, "dumb"=>11.0, "cool"=>15.0, "foo"=>11.0, "bar"=>9.0, "weird"=>8.0}} {"created_at"=>2010-10-02 22:00:00 UTC, "tags"=>{"bar"=>8.0, "dumb"=>8.0, "cool"=>10.0, "foo"=>10.0, "stupid"=>8.0, "weird"=>6.0}} {"created_at"=>2010-09-24 22:00:00 UTC, "tags"=>{"foo"=>13.0, "bar"=>12.0, "stupid"=>15.0, "weird"=>17.0, "dumb"=>7.0, "cool"=>10.0}} {"created_at"=>2010-09-30 22:00:00 UTC, "tags"=>{"bar"=>10.0, "cool"=>6.0, "stupid"=>14.0, "weird"=>9.0, "dumb"=>12.0, "foo"=>19.0}} {"created_at"=>2010-09-05 22:00:00 UTC, "tags"=>{"dumb"=>12.0, "foo"=>19.0, "weird"=>8.0, "stupid"=>8.0, "bar"=>7.0, "cool"=>10.0}} {"created_at"=>2010-09-17 22:00:00 UTC, "tags"=>{"weird"=>13.0, "bar"=>14.0, "dumb"=>12.0, "foo"=>12.0, "stupid"=>10.0, "cool"=>9.0}} {"created_at"=>2010-09-18 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "cool"=>11.0, "foo"=>6.0, "bar"=>12.0, "weird"=>7.0, "stupid"=>6.0}} {"created_at"=>2010-09-09 22:00:00 UTC, "tags"=>{"weird"=>11.0, "dumb"=>9.0, "foo"=>6.0, "bar"=>11.0, "cool"=>11.0, "stupid"=>6.0}} {"created_at"=>2010-09-13 22:00:00 UTC, "tags"=>{"dumb"=>19.0, "stupid"=>9.0, "weird"=>12.0, "cool"=>11.0, "bar"=>10.0, "foo"=>15.0}} {"created_at"=>2010-09-16 22:00:00 UTC, "tags"=>{"bar"=>6.0, "weird"=>8.0, "dumb"=>9.0, "cool"=>11.0, "stupid"=>17.0, "foo"=>15.0}} {"created_at"=>2010-09-11 22:00:00 UTC, "tags"=>{"foo"=>10.0, "weird"=>9.0, "bar"=>8.0, "cool"=>4.0, "dumb"=>8.0, "stupid"=>9.0}} {"created_at"=>2010-09-26 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>6.0, "stupid"=>15.0, "bar"=>10.0, "foo"=>13.0, "cool"=>15.0}} {"created_at"=>2010-10-01 22:00:00 UTC, "tags"=>{"cool"=>7.0, "weird"=>11.0, "stupid"=>11.0, "bar"=>14.0, "foo"=>12.0, "dumb"=>11.0}} {"created_at"=>2010-09-12 22:00:00 UTC, "tags"=>{"bar"=>7.0, "weird"=>12.0, "stupid"=>11.0, "cool"=>10.0, "foo"=>11.0, "dumb"=>9.0}} {"created_at"=>2010-09-14 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "foo"=>15.0, "cool"=>15.0, "stupid"=>15.0, "bar"=>7.0, "weird"=>14.0}} {"created_at"=>2010-09-07 22:00:00 UTC, "tags"=>{"dumb"=>10.0, "cool"=>7.0, "foo"=>14.0, "weird"=>15.0, "bar"=>11.0, "stupid"=>7.0}} {"created_at"=>2010-09-06 22:00:00 UTC, "tags"=>{"dumb"=>7.0, "bar"=>11.0, "cool"=>16.0, "weird"=>14.0, "foo"=>12.0, "stupid"=>6.0}} Donnerstag, 7. Oktober 2010
  121. 121. {"created_at"=>2010-09-19 22:00:00 UTC, "tags"=>{"foo"=>11.0, "dumb"=>12.0, "stupid"=>7.0, "bar"=>7.0, "cool"=>14.0, "weird"=>17.0}} {"created_at"=>2010-09-20 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "stupid"=>5.0, "foo"=>10.0, "cool"=>8.0, "weird"=>9.0, "bar"=>15.0}} {"created_at"=>2010-09-22 22:00:00 UTC, "tags"=>{"weird"=>15.0, "bar"=>9.0, "stupid"=>17.0, "cool"=>11.0, "dumb"=>10.0, "foo"=>12.0}} {"created_at"=>2010-09-15 22:00:00 UTC, "tags"=>{"foo"=>11.0, "weird"=>7.0, "stupid"=>10.0, "cool"=>11.0, "bar"=>5.0, "dumb"=>8.0}} {"created_at"=>2010-09-25 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "weird"=>14.0, "cool"=>8.0, "foo"=>21.0, "bar"=>11.0, "stupid"=>13.0}} {"created_at"=>2010-09-28 22:00:00 UTC, "tags"=>{"cool"=>11.0, "dumb"=>16.0, "stupid"=>11.0, "weird"=>15.0, "foo"=>9.0, "bar"=>16.0}} {"created_at"=>2010-09-10 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>9.0, "bar"=>8.0, "cool"=>14.0, "stupid"=>11.0, "foo"=>11.0}} {"created_at"=>2010-09-03 22:00:00 UTC, "tags" => { "tags"=>{"weird"=>14.0, "dumb"=>15.0, "stupid"=>11.0, "foo"=>16.0, "bar"=>20.0, "cool"=>10.0}} {"created_at"=>2010-09-21 22:00:00 UTC, "tags"=>{"weird"=>15.0, "cool"=>14.0, "foo"=>13.0, "stupid"=>6.0, "bar"=>11.0, "dumb"=>9.0}} {"created_at"=>2010-09-23 {"created_at"=>2010-09-29 22:00:00 22:00:00 UTC, UTC, "foo" => 11.0, "tags"=>{"weird"=>15.0, "stupid"=>15.0, "dumb"=>15.0, "foo"=>16.0, "cool"=>10.0, "bar"=>11.0}} "tags"=>{"bar"=>9.0, "cool"=>14.0, "weird"=>16.0, "foo"=>8.0, "dumb"=>9.0, "stupid"=>12.0}} {"created_at"=>2010-09-27 {"created_at"=>2010-09-04 22:00:00 22:00:00 UTC, UTC, "dumb" => 12.0, "tags"=>{"cool"=>13.0, "dumb"=>10.0, "stupid"=>12.0, "bar"=>8.0, "foo"=>16.0, "weird"=>13.0}} "tags"=>{"cool"=>11.0, "bar"=>9.0, "stupid"=>6.0, "weird"=>11.0, "dumb"=>8.0, "foo"=>11.0}} {"created_at"=>2010-09-08 {"created_at"=>2010-10-02 22:00:00 22:00:00 UTC, UTC, "stupid" => 7.0, "tags"=>{"stupid"=>12.0, "dumb"=>11.0, "cool"=>15.0, "foo"=>11.0, "bar"=>9.0, "weird"=>8.0}} "tags"=>{"bar"=>8.0, "dumb"=>8.0, "cool"=>10.0, "foo"=>10.0, "stupid"=>8.0, "weird"=>6.0}} {"created_at"=>2010-09-24 {"created_at"=>2010-09-30 22:00:00 22:00:00 UTC, UTC, "bar" => 7.0, "tags"=>{"foo"=>13.0, "bar"=>12.0, "stupid"=>15.0, "weird"=>17.0, "dumb"=>7.0, "cool"=>10.0}} "tags"=>{"bar"=>10.0, "cool"=>6.0, "stupid"=>14.0, "weird"=>9.0, "dumb"=>12.0, "foo"=>19.0}} {"created_at"=>2010-09-05 {"created_at"=>2010-09-17 22:00:00 22:00:00 UTC, UTC, "cool" => 14.0, "tags"=>{"dumb"=>12.0, "foo"=>19.0, "weird"=>8.0, "stupid"=>8.0, "bar"=>7.0, "cool"=>10.0}} "tags"=>{"weird"=>13.0, "bar"=>14.0, "dumb"=>12.0, "foo"=>12.0, "stupid"=>10.0, "cool"=>9.0}} {"created_at"=>2010-09-18 {"created_at"=>2010-09-09 22:00:00 22:00:00 UTC, UTC, "weird" => 17.0 "tags"=>{"dumb"=>8.0, "cool"=>11.0, "foo"=>6.0, "bar"=>12.0, "weird"=>7.0, "stupid"=>6.0}} "tags"=>{"weird"=>11.0, "dumb"=>9.0, "foo"=>6.0, "bar"=>11.0, "cool"=>11.0, "stupid"=>6.0}} {"created_at"=>2010-09-13 {"created_at"=>2010-09-16 22:00:00 22:00:00 UTC, UTC, } "tags"=>{"dumb"=>19.0, "stupid"=>9.0, "weird"=>12.0, "cool"=>11.0, "bar"=>10.0, "foo"=>15.0}} "tags"=>{"bar"=>6.0, "weird"=>8.0, "dumb"=>9.0, "cool"=>11.0, "stupid"=>17.0, "foo"=>15.0}} {"created_at"=>2010-09-11 22:00:00 UTC, "tags"=>{"foo"=>10.0, "weird"=>9.0, "bar"=>8.0, "cool"=>4.0, "dumb"=>8.0, "stupid"=>9.0}} {"created_at"=>2010-09-26 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>6.0, "stupid"=>15.0, "bar"=>10.0, "foo"=>13.0, "cool"=>15.0}} {"created_at"=>2010-10-01 22:00:00 UTC, "tags"=>{"cool"=>7.0, "weird"=>11.0, "stupid"=>11.0, "bar"=>14.0, "foo"=>12.0, "dumb"=>11.0}} {"created_at"=>2010-09-12 22:00:00 UTC, "tags"=>{"bar"=>7.0, "weird"=>12.0, "stupid"=>11.0, "cool"=>10.0, "foo"=>11.0, "dumb"=>9.0}} {"created_at"=>2010-09-14 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "foo"=>15.0, "cool"=>15.0, "stupid"=>15.0, "bar"=>7.0, "weird"=>14.0}} {"created_at"=>2010-09-07 22:00:00 UTC, "tags"=>{"dumb"=>10.0, "cool"=>7.0, "foo"=>14.0, "weird"=>15.0, "bar"=>11.0, "stupid"=>7.0}} {"created_at"=>2010-09-06 22:00:00 UTC, "tags"=>{"dumb"=>7.0, "bar"=>11.0, "cool"=>16.0, "weird"=>14.0, "foo"=>12.0, "stupid"=>6.0}} Donnerstag, 7. Oktober 2010
  122. 122. function(prev) { var mostPopular = 0; for(i in prev.tags) { if(prev.tags[i] > mostPopular) { prev.tag = i; prev.count = prev.tags[i]; mostPopular = prev.tags[i]; } } delete prev.tags } Donnerstag, 7. Oktober 2010
  123. 123. {"created_at"=>2010-09-27 22:00:00 UTC, "tag"=>"stupid", "count"=>18.0} {"created_at"=>2010-09-29 22:00:00 UTC, "tag"=>"stupid", "count"=>20.0} {"created_at"=>2010-09-12 22:00:00 UTC, "tag"=>"cool", "count"=>11.0} {"created_at"=>2010-09-04 22:00:00 UTC, "tag"=>"stupid", "count"=>12.0} {"created_at"=>2010-09-21 22:00:00 UTC, "tag"=>"stupid", "count"=>16.0} {"created_at"=>2010-09-03 22:00:00 UTC, "tag"=>"foo", "count"=>15.0} {"created_at"=>2010-09-26 22:00:00 UTC, "tag"=>"foo", "count"=>17.0} {"created_at"=>2010-09-18 22:00:00 UTC, "tag"=>"foo", "count"=>17.0} {"created_at"=>2010-09-24 22:00:00 UTC, "tag"=>"cool", "count"=>11.0} Donnerstag, 7. Oktober 2010
  124. 124. Map / Reduce Donnerstag, 7. Oktober 2010
  125. 125. collection = @db['people'].map_reduce( map, reduce ) collection.find() Donnerstag, 7. Oktober 2010
  126. 126. function() { this.tags.forEach(function(z) { emit(z, {count: 1}); }); } Donnerstag, 7. Oktober 2010
  127. 127. function(key, values) { var total = 0; values.forEach(function(v) { total += v.count }); return {count: total} } Donnerstag, 7. Oktober 2010
  128. 128. Indexes Donnerstag, 7. Oktober 2010
  129. 129. db['people'].create_index("tags") @db['people'].create_index( [["tags", Mongo::ASCENDING]] ) db['people'].drop_index("tags_1") db['people'].drop_indexes db['people'].index_information Donnerstag, 7. Oktober 2010
  130. 130. Geospatial stuff Donnerstag, 7. Oktober 2010
  131. 131. @db['people'].create_index( [["latlng", Mongo::GEO2D]] ) Donnerstag, 7. Oktober 2010
  132. 132. @db['people'].find( "latlng" => {"$near" => [53.593978, 10.107380]} ) Donnerstag, 7. Oktober 2010
  133. 133. GridFS usage Donnerstag, 7. Oktober 2010
  134. 134. grid = Mongo::Grid.new(@db) id = grid.put("You can put Strings in here", :filename => 'test.txt') file = grid.get(id) file.filename file.read grid.delete(id) grid.put( File.open("/Users/jankrutisch/Dropbox/Photos/IMGP8989.jpg") ) Donnerstag, 7. Oktober 2010
  135. 135. fs = Mongo::GridFileSystem.new(db) fs.open("test.txt", "w") do |f| f.write "You can put stuff in here" end fs.open("test.txt", "r") do |f| puts f.read end fs.delete("test.txt") Donnerstag, 7. Oktober 2010
  136. 136. Capped collections Donnerstag, 7. Oktober 2010
  137. 137. @db.create_collection('capped_numbers', :capped => true, :max => 50 ) @db.create_collection('capped_numbers', :capped => true, :size => 1024 * 64 ) Donnerstag, 7. Oktober 2010
  138. 138. explain Donnerstag, 7. Oktober 2010
  139. 139. @db['people'].find( "address.city" => /haven/ ).explain Donnerstag, 7. Oktober 2010
  140. 140. @db['people'].find( "address.city" => /haven/ ).explain Donnerstag, 7. Oktober 2010
  141. 141. { "cursor"=>"BasicCursor", "nscanned"=>1000, "nscannedObjects"=>1000, "n"=>39, "millis"=>2, "indexBounds"=>{}, "allPlans"=>[ {"cursor"=>"BasicCursor", "indexBounds"=>{}} ] } Donnerstag, 7. Oktober 2010
  142. 142. { "cursor"=>"BtreeCursor address.city_1 multi", "nscanned"=>1000, "nscannedObjects"=>39, "n"=>39, "millis"=>1, "indexBounds"=>{ "address.city"=>[["", {}], [/haven/, /haven/]] }, "allPlans"=>[ { "cursor"=>"BtreeCursor address.city_1 multi", "indexBounds"=>{ "address.city"=>[["", {}], [/haven/, /haven/]] } } ] } Donnerstag, 7. Oktober 2010
  143. 143. misc commands Donnerstag, 7. Oktober 2010
  144. 144. @db.stats { "collections"=>4, "objects"=>1011, "avgObjSize"=>244.6330365974283, "dataSize"=>247324, "storageSize"=>345600, "numExtents"=>6, "indexes"=>3, "indexSize"=>114688, "fileSize"=>201326592, "ok"=>1.0 } Donnerstag, 7. Oktober 2010
  145. 145. @db.add_stored_function( "tag_size", "function(obj) {return obj.tags.length}" ) @db['people'].find( "$where" => 'tag_size(this) === 2' ) Donnerstag, 7. Oktober 2010
  146. 146. @db.add_stored_function( "tag_size", "function(obj) {return obj.tags.length}" ) @db['people'].find( "$where" => 'tag_size(this) === 2' ) Donnerstag, 7. Oktober 2010
  147. 147. Libraries Donnerstag, 7. Oktober 2010
  148. 148. mongo_mapper Donnerstag, 7. Oktober 2010
  149. 149. John Nunemaker @jnunemaker Donnerstag, 7. Oktober 2010
  150. 150. is in production Donnerstag, 7. Oktober 2010
  151. 151. documentation? Donnerstag, 7. Oktober 2010
  152. 152. Donnerstag, 7. Oktober 2010
  153. 153. how to Donnerstag, 7. Oktober 2010
  154. 154. rails initializer Donnerstag, 7. Oktober 2010
  155. 155. # config/initializers/mongo_mapper.rb mongo_config = YAML.load_file( File.join(Rails.root, 'config','mongomapper.yml') ) MongoMapper.setup(mongo_config, Rails.env) Donnerstag, 7. Oktober 2010
  156. 156. a simple example Donnerstag, 7. Oktober 2010
  157. 157. MongoMapper.connection = @connection MongoMapper.database = "test" class Quote include MongoMapper::Document key :from key :text key :views, Integer timestamps! end Donnerstag, 7. Oktober 2010
  158. 158. finders Donnerstag, 7. Oktober 2010
  159. 159. Quote.where(:from => 'Yogi Berra').all Quote.where(:from => 'Yogi Berra').limit(5).sort(:from.desc).all Donnerstag, 7. Oktober 2010
  160. 160. embedded docs Donnerstag, 7. Oktober 2010
  161. 161. class Person include MongoMapper::Document key :name one :address key :tags, Array end class Address include MongoMapper::Document key :street key :city key :country key :zip end Donnerstag, 7. Oktober 2010
  162. 162. person = Person.first address = Person.first.address Donnerstag, 7. Oktober 2010
  163. 163. scopes Donnerstag, 7. Oktober 2010
  164. 164. class Person scope :tagged, lambda { |tag| where(:tags.in => [tag]) } end puts Person.tagged('cool').first.inspect Donnerstag, 7. Oktober 2010
  165. 165. new website coming soon Donnerstag, 7. Oktober 2010
  166. 166. new website coming soon U l re ad y s a id t h at n f o rt u n ate l y I‘ve a ug us t @ FrOS CON i n A Donnerstag, 7. Oktober 2010
  167. 167. new website coming soon U l re ad y s a id t h at n f o rt u n ate l y I‘ve a ug us t @ FrOS CON i n A Donnerstag, 7. Oktober 2010
  168. 168. mongoid Donnerstag, 7. Oktober 2010
  169. 169. Durran Jordan (of Hashrocket) Donnerstag, 7. Oktober 2010
  170. 170. Two major versions Donnerstag, 7. Oktober 2010
  171. 171. 1.x (1.9.x) targeting Rails 2.3.x Donnerstag, 7. Oktober 2010
  172. 172. 2.x (2.0beta) targeting Rails 3.0 Donnerstag, 7. Oktober 2010
  173. 173. Good documentation Donnerstag, 7. Oktober 2010
  174. 174. Donnerstag, 7. Oktober 2010
  175. 175. rails initializer Donnerstag, 7. Oktober 2010
  176. 176. File.open(File.join(RAILS_ROOT, 'config/mongodb.yml'), 'r') do |f| @settings = YAML.load(f)[RAILS_ENV] end Mongoid::Config.instance.from_hash(@settings) Donnerstag, 7. Oktober 2010
  177. 177. a simple example Donnerstag, 7. Oktober 2010
  178. 178. class Quote include Mongoid::Document include Mongoid::Timestamps field :from field :text field :views, :type => Integer end Donnerstag, 7. Oktober 2010
  179. 179. finders Donnerstag, 7. Oktober 2010
  180. 180. Quote.where(:from => 'Yogi Berra').all Quote.where(:from => 'Yogi Berra').limit(5).order_by(:from.desc).all Donnerstag, 7. Oktober 2010
  181. 181. embedded docs Donnerstag, 7. Oktober 2010
  182. 182. class Person include Mongoid::Document field :name embeds_one :address field :tags, :type => Array end class Address include Mongoid::Document field :street field :city field :country field :zip end Donnerstag, 7. Oktober 2010
  183. 183. person = Person.first address = Person.first.address Donnerstag, 7. Oktober 2010
  184. 184. scopes Donnerstag, 7. Oktober 2010
  185. 185. class Person scope :tagged, lambda { |tag| where(:tags.in => [tag]) } end puts Person.tagged('cool').first.inspect Donnerstag, 7. Oktober 2010
  186. 186. More features Donnerstag, 7. Oktober 2010
  187. 187. atomic updates Donnerstag, 7. Oktober 2010
  188. 188. mongoid tries to be clever Donnerstag, 7. Oktober 2010
  189. 189. (using the „dirty“ flags) Donnerstag, 7. Oktober 2010
  190. 190. (it‘s probably better to bypass the ODM sometimes) Donnerstag, 7. Oktober 2010
  191. 191. GridFS Donnerstag, 7. Oktober 2010
  192. 192. external libraries for both Donnerstag, 7. Oktober 2010
  193. 193. mongo_mapper > grip Donnerstag, 7. Oktober 2010
  194. 194. mongoid > mongoid_grid Donnerstag, 7. Oktober 2010
  195. 195. validations Donnerstag, 7. Oktober 2010
  196. 196. both through ActiveModel or validatable (Rails <3) Donnerstag, 7. Oktober 2010
  197. 197. Other noteworthy libraries Donnerstag, 7. Oktober 2010
  198. 198. The „ORM“ variant Donnerstag, 7. Oktober 2010
  199. 199. mongodoc http://github.com/leshill/mongodoc Donnerstag, 7. Oktober 2010
  200. 200. mongo-record http://github.com/mongodb/mongo-record Donnerstag, 7. Oktober 2010
  201. 201. mongomodel http://github.com/spohlenz/mongomodel Donnerstag, 7. Oktober 2010
  202. 202. mongomatic http://github.com/benmyles/mongomatic Donnerstag, 7. Oktober 2010
  203. 203. Queues Donnerstag, 7. Oktober 2010
  204. 204. mongo_queue http://github.com/Skiz/mongo_queue Donnerstag, 7. Oktober 2010
  205. 205. mongo_queue L as t c omm i t o n g i t h ub.c om i n M a rch .. . http://github.com/Skiz/mongo_queue Donnerstag, 7. Oktober 2010
  206. 206. resque-mongo http://github.com/ctrochalakis/resque-mongo Donnerstag, 7. Oktober 2010
  207. 207. resque-mongo L as t c omm i t o n g i t h ub.c om i n Ja n u a r y.. . http://github.com/ctrochalakis/resque-mongo Donnerstag, 7. Oktober 2010
  208. 208. Misc Donnerstag, 7. Oktober 2010
  209. 209. em-mongo http://github.com/bcg/em-mongo Donnerstag, 7. Oktober 2010
  210. 210. dm-mongo-adapter http://github.com/solnic/dm-mongo-adapter Donnerstag, 7. Oktober 2010
  211. 211. questions? injuries? Donnerstag, 7. Oktober 2010
  212. 212. I Donnerstag, 7. Oktober 2010
  213. 213. thanks for listening. ‣ http://www.mongodb.org/ ‣ http://www.mongoid.org/ ‣ http://github.com/jnunemaker/mongo_mapper ‣ http://github.com/halfbyte/mongo_ruby_examples ‣ jan@krutisch.de ‣ http://jan.krutisch.de/ ‣ http://github.com/halfbyte/ ‣ http://twitter.com/halfbyte ‣ http://www.mindmatters.de/ Donnerstag, 7. Oktober 2010

×