Mongodb on Ruby And Rails (froscon 2010)

3,483 views

Published on

Slides to my talk at the FrosCon in St. Augustin

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,483
On SlideShare
0
From Embeds
0
Number of Embeds
100
Actions
Shares
0
Downloads
42
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Mongodb on Ruby And Rails (froscon 2010)

  1. 1. on rails froscon 2010, st. augustin. jan krutisch <jan@krutisch.de> http://jan.krutisch.de/ Sonntag, 22. August 2010
  2. 2. mongodb wtf? lol!? Sonntag, 22. August 2010
  3. 3. document database Sonntag, 22. August 2010
  4. 4. NoS QL inclu ded! document database Sonntag, 22. August 2010
  5. 5. 10gen Sonntag, 22. August 2010
  6. 6. open source http://github.com/mongodb/mongo Sonntag, 22. August 2010
  7. 7. id title descr pos_lat pos_lng Sonntag, 22. August 2010
  8. 8. no fixed schema no migrations Sonntag, 22. August 2010
  9. 9. rich data structure Sonntag, 22. August 2010
  10. 10. { "_id" : ObjectId("4c00245062610475a005afcd"), "address" : "Bernstorffstr. 174n22767 HamburgnDE", "description" : null, "position" : { "lat" : 53.5600912, "lng" : 9.9596977 }, "tags" : [ "hausarzt", "naturheilverfahren", "akupunktur", "allgemeinmedizin" ], "title" : "Dr. med. Lilo Eisenbarth", "loxicon_id" : 808261 } Sonntag, 22. August 2010
  11. 11. Sonntag, 22. August 2010
  12. 12. Sonntag, 22. August 2010 ✗
  13. 13. Sonntag, 22. August 2010
  14. 14. BSON Sonntag, 22. August 2010
  15. 15. BInary Serialized jsON http://bsonspec.org/ Sonntag, 22. August 2010
  16. 16. Lightweight Sonntag, 22. August 2010
  17. 17. Traversable Sonntag, 22. August 2010
  18. 18. Efficient Sonntag, 22. August 2010
  19. 19. Wire Sonntag, 22. August 2010
  20. 20. Storage Sonntag, 22. August 2010
  21. 21. rich queries Sonntag, 22. August 2010
  22. 22. conceptually close to SQL Sonntag, 22. August 2010
  23. 23. easy to grasp Sonntag, 22. August 2010
  24. 24. flexible Sonntag, 22. August 2010
  25. 25. language integration Sonntag, 22. August 2010
  26. 26. on top: map/reduce Sonntag, 22. August 2010
  27. 27. Scaling Sonntag, 22. August 2010
  28. 28. Master/Slave replication Sonntag, 22. August 2010
  29. 29. Replica Sets (1.6) Sonntag, 22. August 2010
  30. 30. Primary Member Member Sonntag, 22. August 2010
  31. 31. Primary Member Primary Sonntag, 22. August 2010
  32. 32. Member Member Primary Sonntag, 22. August 2010
  33. 33. Autosharding (1.6) Sonntag, 22. August 2010
  34. 34. Sonntag, 22. August 2010
  35. 35. Durability Sonntag, 22. August 2010
  36. 36. No single server durability! Sonntag, 22. August 2010
  37. 37. fsyncs every 60s (configurable) Sonntag, 22. August 2010
  38. 38. Use Replication! Sonntag, 22. August 2010
  39. 39. Use write propagation locking Sonntag, 22. August 2010
  40. 40. Single Server Durability planned for 1.8 Sonntag, 22. August 2010
  41. 41. mongo console Sonntag, 22. August 2010
  42. 42. $ mongo Sonntag, 22. August 2010
  43. 43. > use test switched to db test db.quotes.save({ text: "You can observe a lot just by watching.", from: "Yogi Berra", created_at: new Date() }); db.quotes.save({ text: "Silence is one of the hardest arguments to refute.", from: "Josh Billings", created_at: new Date() }); Sonntag, 22. August 2010
  44. 44. Indexing Sonntag, 22. August 2010
  45. 45. Same concept as with SQL databases Sonntag, 22. August 2010
  46. 46. You want them Sonntag, 22. August 2010
  47. 47. Same concept as with SQL databases Sonntag, 22. August 2010
  48. 48. Sort order Sonntag, 22. August 2010
  49. 49. Unique Sonntag, 22. August 2010
  50. 50. Compound Sonntag, 22. August 2010
  51. 51. Geospatial Sonntag, 22. August 2010
  52. 52. map/reduce Sonntag, 22. August 2010
  53. 53. we can haz it, too Sonntag, 22. August 2010
  54. 54. function() { this.tags.forEach(function(z) { emit(z, {count: 1}); }); } function(key, values) { var total = 0; values.forEach(function(v) { total += v }); return {count: total} } Sonntag, 22. August 2010
  55. 55. (it‘s not fast...) Sonntag, 22. August 2010
  56. 56. security Sonntag, 22. August 2010
  57. 57. simple user/password auth Sonntag, 22. August 2010
  58. 58. per database Sonntag, 22. August 2010
  59. 59. read only is possible Sonntag, 22. August 2010
  60. 60. one more thing Sonntag, 22. August 2010
  61. 61. GridFS Sonntag, 22. August 2010
  62. 62. Binary fields in BSON < 4MB Sonntag, 22. August 2010
  63. 63. GridFS saves files in chunks Sonntag, 22. August 2010
  64. 64. I‘m in u‘r rubies, querying teh MongoDB! Sonntag, 22. August 2010
  65. 65. core driver Sonntag, 22. August 2010
  66. 66. mongo / bson_ext Sonntag, 22. August 2010
  67. 67. ODMs / Libs Sonntag, 22. August 2010
  68. 68. mongo_mapper Sonntag, 22. August 2010
  69. 69. mongoid Sonntag, 22. August 2010
  70. 70. Find examples here: http://github.com/halfbyte/mongo_ruby_examples Sonntag, 22. August 2010
  71. 71. Basic driver usage Sonntag, 22. August 2010
  72. 72. init Sonntag, 22. August 2010
  73. 73. require 'mongo' @connection = Mongo::Connection.new @db = @connection.db("test") Sonntag, 22. August 2010
  74. 74. insert/upsert Sonntag, 22. August 2010
  75. 75. doc = { :text => "You can observe a lot just by watching.", :from => "Yogi Berra", :created_at => Time.now } @db['quotes'].insert(doc) Sonntag, 22. August 2010
  76. 76. doc = @db['quotes'].find_one(id) doc[:from] = "Yogi Berra, famous baseball player" @db['quotes'].save(doc) Sonntag, 22. August 2010
  77. 77. atomic updates Sonntag, 22. August 2010
  78. 78. @db['quotes'].update( {"from" => "Yogi Berra"}, {"$inc" => {"reads" => 1 } } ) Sonntag, 22. August 2010
  79. 79. @db['quotes'].update( {"from" => "Yogi Berra"}, {"$inc" => {"reads" => 1 } } ) Sonntag, 22. August 2010
  80. 80. $inc $addToSet $set $pop $unset $pull $push $pullAll $pushAll $ Sonntag, 22. August 2010
  81. 81. getting a whole collection Sonntag, 22. August 2010
  82. 82. @db['quotes'].find.each do |row| puts row.inspect end Sonntag, 22. August 2010
  83. 83. exact query Sonntag, 22. August 2010
  84. 84. @db['quotes'].find(:from => "Yogi Berra") Sonntag, 22. August 2010
  85. 85. more queries Sonntag, 22. August 2010
  86. 86. 100.times do |i| db['numbers'].insert({"i" => i}) end Sonntag, 22. August 2010
  87. 87. db['numbers'].find("i" => {"$lt" => 2}) Sonntag, 22. August 2010
  88. 88. $lt < $gt > $lte <= $gte >= $ne != Sonntag, 22. August 2010
  89. 89. @db['people'].find(:tags => {"$in" => ['cool']}) Sonntag, 22. August 2010
  90. 90. obj = { "_id"=>BSON::ObjectID('4c706af16261040680000369'), "name"=>"Vernon Kreiger", "address"=>{ "street"=>"536 Haleigh Locks", "city"=>"Port Kiannahaven", "zip"=>"80730-0214", "country"=>"Fakistan" }, "tags"=>["cool", "weird"] } Sonntag, 22. August 2010
  91. 91. obj = { "_id"=>BSON::ObjectID('4c706af16261040680000369'), "name"=>"Vernon Kreiger", "address"=>{ "street"=>"536 Haleigh Locks", "city"=>"Port Kiannahaven", "zip"=>"80730-0214", "country"=>"Fakistan" }, "tags"=>["cool", "weird"] } Sonntag, 22. August 2010
  92. 92. $in IN (2,3,4) $nin NOT IN $all [2,3] ~ [1,2,3] Sonntag, 22. August 2010
  93. 93. $mod yah, RLY $size okay $exists NOT NULL $type huh? Sonntag, 22. August 2010
  94. 94. @db['people'].find("address.city" => /haven/) Sonntag, 22. August 2010
  95. 95. @db['people'].find("address.city" => /haven/) Sonntag, 22. August 2010
  96. 96. Sorting Sonntag, 22. August 2010
  97. 97. @db['people'].find().sort("address.street") Sonntag, 22. August 2010
  98. 98. @db['people'].find().sort("address.street") Sonntag, 22. August 2010
  99. 99. Pagination Sonntag, 22. August 2010
  100. 100. @db['numbers'].find.sort("i").limit(10) Sonntag, 22. August 2010
  101. 101. @db['numbers'].find.sort("i").limit(10).skip(50) Sonntag, 22. August 2010
  102. 102. Counting Sonntag, 22. August 2010
  103. 103. @db['numbers'].find.count Sonntag, 22. August 2010
  104. 104. Distinct Sonntag, 22. August 2010
  105. 105. @db['people'].distinct('tags').inspect Sonntag, 22. August 2010
  106. 106. Group Sonntag, 22. August 2010
  107. 107. Poor mans map/reduce Sonntag, 22. August 2010
  108. 108. Map / Reduce Sonntag, 22. August 2010
  109. 109. map = <<-END function() { this.tags.forEach(function(z) { emit(z, {count: 1}); }); } END reduce = <<-END function(key, values) { var total = 0; values.forEach(function(v) { total += v.count }); return {count: total} } END collection = @db['people'].map_reduce( map, reduce ) Sonntag, 22. August 2010
  110. 110. Indexes Sonntag, 22. August 2010
  111. 111. db['people'].create_index("tags") db['people'].drop_index("tags_1") db['people'].index_information Sonntag, 22. August 2010
  112. 112. GridFS usage Sonntag, 22. August 2010
  113. 113. 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") ) Sonntag, 22. August 2010
  114. 114. 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") Sonntag, 22. August 2010
  115. 115. Capped collections Sonntag, 22. August 2010
  116. 116. @db.create_collection('capped_numbers', :capped => true, :max => 50 ) @db.create_collection('capped_numbers', :capped => true, :size => 1024 * 64 ) Sonntag, 22. August 2010
  117. 117. ODMs Sonntag, 22. August 2010
  118. 118. mongo_mapper Sonntag, 22. August 2010
  119. 119. John Nunemaker @jnunemaker Sonntag, 22. August 2010
  120. 120. is in production Sonntag, 22. August 2010
  121. 121. documentation? Sonntag, 22. August 2010
  122. 122. Sonntag, 22. August 2010
  123. 123. how to Sonntag, 22. August 2010
  124. 124. rails initializer Sonntag, 22. August 2010
  125. 125. # config/initializers/mongo_mapper.rb File.open(File.join(Rails.root, 'config/mongodb.yml'), 'r') do |f| @settings = YAML.load(f)[Rails.env] end MongoMapper.connection = Mongo::Connection.from_uri(@settings["connection"]) if @settings["connection"] MongoMapper.database = @settings["database"] Sonntag, 22. August 2010
  126. 126. a simple example Sonntag, 22. August 2010
  127. 127. MongoMapper.connection = @connection MongoMapper.database = "test" class Quote include MongoMapper::Document key :from key :text key :views, Integer timestamps! end Sonntag, 22. August 2010
  128. 128. finders Sonntag, 22. August 2010
  129. 129. Quote.where(:from => 'Yogi Berra').all Quote.where(:from => 'Yogi Berra').limit(5).sort(:from.desc).all Sonntag, 22. August 2010
  130. 130. embedded docs Sonntag, 22. August 2010
  131. 131. 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 Sonntag, 22. August 2010
  132. 132. person = Person.first address = Person.first.address Sonntag, 22. August 2010
  133. 133. scopes Sonntag, 22. August 2010
  134. 134. class Person scope :tagged, lambda { |tag| where(:tags.in => [tag]) } end puts Person.tagged('cool').first.inspect Sonntag, 22. August 2010
  135. 135. new website coming soon Sonntag, 22. August 2010
  136. 136. mongoid Sonntag, 22. August 2010
  137. 137. Durran Jordan (of Hashrocket) Sonntag, 22. August 2010
  138. 138. Two major versions Sonntag, 22. August 2010
  139. 139. 1.x (1.9.x) targeting Rails 2.3.x Sonntag, 22. August 2010
  140. 140. 2.x (2.0beta) targeting Rails 3.0 Sonntag, 22. August 2010
  141. 141. Good documentation Sonntag, 22. August 2010
  142. 142. Sonntag, 22. August 2010
  143. 143. rails initializer Sonntag, 22. August 2010
  144. 144. 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) Sonntag, 22. August 2010
  145. 145. a simple example Sonntag, 22. August 2010
  146. 146. class Quote include Mongoid::Document include Mongoid::Timestamps field :from field :text field :views, :type => Integer end Sonntag, 22. August 2010
  147. 147. finders Sonntag, 22. August 2010
  148. 148. Quote.where(:from => 'Yogi Berra').all Quote.where(:from => 'Yogi Berra').limit(5).order_by(:from.desc).all Sonntag, 22. August 2010
  149. 149. embedded docs Sonntag, 22. August 2010
  150. 150. 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 Sonntag, 22. August 2010
  151. 151. person = Person.first address = Person.first.address Sonntag, 22. August 2010
  152. 152. scopes Sonntag, 22. August 2010
  153. 153. class Person scope :tagged, lambda { |tag| where(:tags.in => [tag]) } end puts Person.tagged('cool').first.inspect Sonntag, 22. August 2010
  154. 154. More features Sonntag, 22. August 2010
  155. 155. atomic updates Sonntag, 22. August 2010
  156. 156. mongoid tries to be clever Sonntag, 22. August 2010
  157. 157. (using the „dirty“ flags) Sonntag, 22. August 2010
  158. 158. (it‘s probably better to bypass the ODM sometimes) Sonntag, 22. August 2010
  159. 159. GridFS Sonntag, 22. August 2010
  160. 160. external libraries for both Sonntag, 22. August 2010
  161. 161. mongo_mapper > grip Sonntag, 22. August 2010
  162. 162. mongoid > mongoid_grid Sonntag, 22. August 2010
  163. 163. Other noteworthy libraries Sonntag, 22. August 2010
  164. 164. Candy Sonntag, 22. August 2010
  165. 165. Candy Sonntag, 22. August 2010
  166. 166. mongodoc http://github.com/leshill/mongodoc Sonntag, 22. August 2010
  167. 167. mongo-record http://github.com/mongodb/mongo-record Sonntag, 22. August 2010
  168. 168. mongomodel http://github.com/spohlenz/mongomodel Sonntag, 22. August 2010
  169. 169. mongo_queue http://github.com/Skiz/mongo_queue Sonntag, 22. August 2010
  170. 170. resque-mongo http://github.com/ctrochalakis/resque-mongo Sonntag, 22. August 2010
  171. 171. my account Sonntag, 22. August 2010
  172. 172. Installation was easy Sonntag, 22. August 2010
  173. 173. (when on right platform) Sonntag, 22. August 2010
  174. 174. setting up replication Sonntag, 22. August 2010
  175. 175. $ mongod --master or master = true # mongodb.conf $ mongod --slave --source slaveserver.example.com slave = true source = slaveserver.example.com Sonntag, 22. August 2010
  176. 176. (see example on github) Sonntag, 22. August 2010
  177. 177. OpLog size! Sonntag, 22. August 2010
  178. 178. „security“ Sonntag, 22. August 2010
  179. 179. memory usage? Sonntag, 22. August 2010
  180. 180. limits? Sonntag, 22. August 2010
  181. 181. stability? Sonntag, 22. August 2010
  182. 182. I Sonntag, 22. August 2010
  183. 183. 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/ Sonntag, 22. August 2010

×