Mongo db

1,278 views
1,204 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,278
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mongo db

  1. 1. MongoDB Forward Tech Away Day 24 Sep 2010 toni@forward.co.uk Monday, 27 September 2010
  2. 2. 1 Get to know MongoDB Monday, 27 September 2010
  3. 3. try.mongodb.org Monday, 27 September 2010
  4. 4. Ruby driver require 'rubygems' require 'mongo' @db = Mongo::Connection.new.db("blog") @blogposts = @db['blogposts'] Monday, 27 September 2010
  5. 5. a simple document post = { :time => Time.now.utc, :title => "Simple Post", } @blogposts << blogpost Monday, 27 September 2010
  6. 6. When we call #save: 1. Adds an _id 2. Serialise to BSON 3. socket.send() Monday, 27 September 2010
  7. 7. 2 Use rich documents Monday, 27 September 2010
  8. 8. “complex objects” blogpost = { :title => "My First Post", :author => {:name => "Jane"}, :comments => [{ :by => "Abe", :text => "First" }, { :by => "Ada", :text => "Good post" }] } @blogposts.save(blogpost) Monday, 27 September 2010
  9. 9. “complex objects” blogpost = { :title => "My First Post", :time => Time.now.utc, :author => {:name => "Jane"}, :views => 0, :languages => ["English", "Italian", "Spanish"], :comments => [{ :by => "Abe", :text => "First", :vote => 1, :date => Time.now.utc }] } @blogposts.save(blogpost) Monday, 27 September 2010
  10. 10. dynamic queries @blogposts.find( { "author.name" => "Jane" } ) @blogposts.find( { "author.name" => /^J/ } ) @blogposts.find( { :time => {'$lte' => Time.utc(1970,1,1)} } ) @blogposts.find( { :languages => {'$in' => ["English", "Italian"] } } ) Monday, 27 September 2010
  11. 11. find and modify! cmd = BSON::OrderedHash.new cmd[:findandmodify] = 'blogposts' cmd[:query] = { :title => "My First Post" } cmd[:update] = { '$set' => { :title => "My First Post updated"}} DB.command(cmd) Monday, 27 September 2010
  12. 12. upsert @blogposts.update({:title => "My First Post"}, blogpost, { :upsert => true }) Monday, 27 September 2010
  13. 13. update @blogposts.update({:title => "My First Post"}, blogpost) @blogposts.update( {:title => "My First Post"}, {'$inc' => {"views" => 1}}) Monday, 27 September 2010
  14. 14. query operators "$ne" "$inc" "$in" "$set" "$nin" "$push" "$mod" "$pushAll" "$all" "$pop" "$size" "$pull" "$exists" "$pullAll" Monday, 27 September 2010
  15. 15. 3 Map reduce for aggregation Monday, 27 September 2010
  16. 16. map reduce def self.target_for_report(report) collections = DB['targets'].group(['metric'], { :report_type => report.report_type, :project_name => report.project }, { 'docs' => [] }, "function(doc, prev) { prev.docs.push(doc); }"); end Monday, 27 September 2010
  17. 17. 4 Indexes are indexes Monday, 27 September 2010
  18. 18. indexes @blogposts.create_index([[ "comments.by" , Mongo::ASCENDING ]]); db.blogposts.ensureIndex( { "comments.by" : 1 } ); @blogposts.create_index([[ :languages , Mongo::ASCENDING ]]); @blogposts.create_index([[ :time , Mongo::DESCENDING ]]); @blogposts.create_index([[ :author , Mongo::ASCENDING, true ]]); @blogposts.create_index( [[ "comments.vote" , 1,], [ "comments.date", -1 ]]); Monday, 27 September 2010
  19. 19. 5 GridFS Monday, 27 September 2010
  20. 20. api grid = Mongo::Grid.new(DB) my_avatar = File.open('toni.jpg','r') id = grid.put(my_avatar) files collection for metadata chunks collection for data Monday, 27 September 2010
  21. 21. 6 Replication Monday, 27 September 2010
  22. 22. m/s vs replica set asynchronous replication of data between servers for failover and redundancy only one server (in the set/shard) is active for writes (the primary, or master) at a given time. Monday, 27 September 2010
  23. 23. replica sets (1.6+) • Supports 1-7 servers in the cluster • Automatic failover • Automatic recovery Monday, 27 September 2010
  24. 24. 7 Auto-shard Monday, 27 September 2010
  25. 25. mongo shards Sharding occurs on a per-collection basis Monday, 27 September 2010
  26. 26. 8 Pro & Cons Monday, 27 September 2010
  27. 27. pro & cons • schema free > no migrations needed • no sql • update existing data • stable • very nice api Monday, 27 September 2010
  28. 28. good for • the web • real time • logging • analytics • humans Monday, 27 September 2010
  29. 29. 9 Links Monday, 27 September 2010
  30. 30. links • http://api.mongodb.org/ruby/1.0.9/ index.html • http://www.mongodb.org/ • http://mongoid.org/ Monday, 27 September 2010
  31. 31. 10 Questions Monday, 27 September 2010

×