SlideShare a Scribd company logo
MongoDB      Forward Tech Away Day 24 Sep 2010

                                   toni@forward.co.uk




Monday, 27 September 2010
1
  Get to know
  MongoDB
Monday, 27 September 2010
try.mongodb.org




Monday, 27 September 2010
Ruby driver
          require 'rubygems'
          require 'mongo'

          @db = Mongo::Connection.new.db("blog")

          @blogposts = @db['blogposts']




Monday, 27 September 2010
a simple document

               post = {
                 :time => Time.now.utc,
                 :title => "Simple Post",
               }

               @blogposts << blogpost




Monday, 27 September 2010
When we call #save:
               1. Adds an _id
               2. Serialise to BSON
               3. socket.send()


Monday, 27 September 2010
2
  Use rich
  documents
Monday, 27 September 2010
“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
“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
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
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
upsert

       @blogposts.update({:title => "My First Post"}, blogpost,
         { :upsert => true })




Monday, 27 September 2010
update
          @blogposts.update({:title => "My First Post"}, blogpost)

          @blogposts.update(
            {:title => "My First Post"}, {'$inc' => {"views" => 1}})




Monday, 27 September 2010
query operators

                            "$ne"       "$inc"
                            "$in"       "$set"
                            "$nin"      "$push"
                            "$mod"      "$pushAll"
                            "$all"      "$pop"
                            "$size"     "$pull"
                            "$exists"   "$pullAll"




Monday, 27 September 2010
3
  Map reduce for
  aggregation
Monday, 27 September 2010
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
4
  Indexes are
  indexes
Monday, 27 September 2010
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
5
  GridFS
Monday, 27 September 2010
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
6
 Replication
Monday, 27 September 2010
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
replica sets (1.6+)

                   • Supports 1-7 servers in the cluster
                   • Automatic failover
                   • Automatic recovery


Monday, 27 September 2010
7
  Auto-shard
Monday, 27 September 2010
mongo shards




              Sharding occurs on a per-collection basis




Monday, 27 September 2010
8
  Pro & Cons
Monday, 27 September 2010
pro & cons

                   • schema free > no migrations needed
                   • no sql
                   • update existing data
                   • stable
                   • very nice api

Monday, 27 September 2010
good for

                   • the web
                   • real time
                   • logging
                   • analytics
                   • humans

Monday, 27 September 2010
9
  Links
Monday, 27 September 2010
links

                   • http://api.mongodb.org/ruby/1.0.9/
                            index.html
                   • http://www.mongodb.org/
                   • http://mongoid.org/


Monday, 27 September 2010
10
  Questions
Monday, 27 September 2010

More Related Content

PDF
Learning jQuery in 30 minutes
PDF
An introduction to the create js suite
PDF
Intro to HTML5 Web Storage
PDF
Getting Started with MongoDB
PDF
Developing in HTML5: Widgetbox & Sencha Touch
PDF
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
PDF
Intro to HTML5 Game Programming
KEY
C# Development (Sam Corder)
Learning jQuery in 30 minutes
An introduction to the create js suite
Intro to HTML5 Web Storage
Getting Started with MongoDB
Developing in HTML5: Widgetbox & Sencha Touch
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Intro to HTML5 Game Programming
C# Development (Sam Corder)

What's hot (7)

PDF
Best Practices - Mobile Developer Summit
PDF
Chrome拡張開発者のためのFirefox拡張開発
PPT
Yuihacku iitd-sumana
PPTX
Learn JS concepts by implementing jQuery
PDF
API Design
PDF
Drupal 7: What's In It For You?
PDF
State of jQuery and Drupal
Best Practices - Mobile Developer Summit
Chrome拡張開発者のためのFirefox拡張開発
Yuihacku iitd-sumana
Learn JS concepts by implementing jQuery
API Design
Drupal 7: What's In It For You?
State of jQuery and Drupal
Ad

Viewers also liked (16)

PPTX
SASPAC 2011 and beyond - BURISA Conference 13.05.11
KEY
Blend it up - leancamp london presentation
PDF
Festa Dei Sozzi
ODP
PPTX
PPTX
在Sae上开发高性能微博应用
PPT
Rachel & Richards Wedding Slide Show
PDF
Autotools
PPT
Publizitatearen Historia 3gaia2/3zatia
KEY
UX Must Die
PPT
Team Work!!!
PDF
Presentatie Mariska van Zelst-de Wit Masterclass Open Cultuur Data
PPT
Ict4volunteering Mv
PPT
2011 Ons Data Workshop (20.3.07)
PPTX
Asbury Hadoop Overview
PPT
Ocd workshop de_effecten_van_ocd
SASPAC 2011 and beyond - BURISA Conference 13.05.11
Blend it up - leancamp london presentation
Festa Dei Sozzi
在Sae上开发高性能微博应用
Rachel & Richards Wedding Slide Show
Autotools
Publizitatearen Historia 3gaia2/3zatia
UX Must Die
Team Work!!!
Presentatie Mariska van Zelst-de Wit Masterclass Open Cultuur Data
Ict4volunteering Mv
2011 Ons Data Workshop (20.3.07)
Asbury Hadoop Overview
Ocd workshop de_effecten_van_ocd
Ad

Similar to Mongo db (20)

PDF
Persisting dynamic data with mongodb and mongomapper
KEY
MongoDB at RubyEnRails 2009
PDF
MongoDB at FrozenRails
KEY
Introduction to MongoDB
KEY
MongoDB at RuPy
PDF
Everyday - mongodb
KEY
MongoDB at ZPUGDC
KEY
Schema Design by Example ~ MongoSF 2012
PDF
Ruby on CouchDB - SimplyStored and RockingChair
KEY
MongoDB
PDF
Intro To MongoDB
PDF
Introduction to MongoDB
KEY
2012 phoenix mug
KEY
Nosql redis-mongo
PDF
Persistence Smoothie: Blending SQL and NoSQL (RubyNation Edition)
PDF
Pyconie 2012
PDF
Starting with MongoDB
KEY
PDF
Latinoware
PPTX
Realtime Analytics with MongoDB Counters (mongonyc 2012)
Persisting dynamic data with mongodb and mongomapper
MongoDB at RubyEnRails 2009
MongoDB at FrozenRails
Introduction to MongoDB
MongoDB at RuPy
Everyday - mongodb
MongoDB at ZPUGDC
Schema Design by Example ~ MongoSF 2012
Ruby on CouchDB - SimplyStored and RockingChair
MongoDB
Intro To MongoDB
Introduction to MongoDB
2012 phoenix mug
Nosql redis-mongo
Persistence Smoothie: Blending SQL and NoSQL (RubyNation Edition)
Pyconie 2012
Starting with MongoDB
Latinoware
Realtime Analytics with MongoDB Counters (mongonyc 2012)

More from Antonio Terreno (11)

PDF
Serverless conference-labrador-at-2018
PDF
Programmer Anarchy
KEY
Socket.io
PDF
Agiler without a schema @forward
PPT
J2Me Il Micro Mondo Java
PDF
Domain Driven Design Javaday Roma2007
PDF
Jc06 Antonio Terreno Fluidtime
PDF
PDF
From Amber To Green in Four Weeks
PPT
Time Boxing
PPT
Importance Of Being Driven
Serverless conference-labrador-at-2018
Programmer Anarchy
Socket.io
Agiler without a schema @forward
J2Me Il Micro Mondo Java
Domain Driven Design Javaday Roma2007
Jc06 Antonio Terreno Fluidtime
From Amber To Green in Four Weeks
Time Boxing
Importance Of Being Driven

Mongo db

  • 1. MongoDB Forward Tech Away Day 24 Sep 2010 toni@forward.co.uk Monday, 27 September 2010
  • 2. 1 Get to know MongoDB Monday, 27 September 2010
  • 4. Ruby driver require 'rubygems' require 'mongo' @db = Mongo::Connection.new.db("blog") @blogposts = @db['blogposts'] Monday, 27 September 2010
  • 5. a simple document post = { :time => Time.now.utc, :title => "Simple Post", } @blogposts << blogpost Monday, 27 September 2010
  • 6. When we call #save: 1. Adds an _id 2. Serialise to BSON 3. socket.send() Monday, 27 September 2010
  • 7. 2 Use rich documents Monday, 27 September 2010
  • 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. “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. 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. 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. upsert @blogposts.update({:title => "My First Post"}, blogpost, { :upsert => true }) Monday, 27 September 2010
  • 13. update @blogposts.update({:title => "My First Post"}, blogpost) @blogposts.update( {:title => "My First Post"}, {'$inc' => {"views" => 1}}) Monday, 27 September 2010
  • 14. query operators "$ne" "$inc" "$in" "$set" "$nin" "$push" "$mod" "$pushAll" "$all" "$pop" "$size" "$pull" "$exists" "$pullAll" Monday, 27 September 2010
  • 15. 3 Map reduce for aggregation Monday, 27 September 2010
  • 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. 4 Indexes are indexes Monday, 27 September 2010
  • 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. 5 GridFS Monday, 27 September 2010
  • 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. 6 Replication Monday, 27 September 2010
  • 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. replica sets (1.6+) • Supports 1-7 servers in the cluster • Automatic failover • Automatic recovery Monday, 27 September 2010
  • 24. 7 Auto-shard Monday, 27 September 2010
  • 25. mongo shards Sharding occurs on a per-collection basis Monday, 27 September 2010
  • 26. 8 Pro & Cons Monday, 27 September 2010
  • 27. pro & cons • schema free > no migrations needed • no sql • update existing data • stable • very nice api Monday, 27 September 2010
  • 28. good for • the web • real time • logging • analytics • humans Monday, 27 September 2010
  • 29. 9 Links Monday, 27 September 2010
  • 30. links • http://api.mongodb.org/ruby/1.0.9/ index.html • http://www.mongodb.org/ • http://mongoid.org/ Monday, 27 September 2010
  • 31. 10 Questions Monday, 27 September 2010