0
Stash the Hash!MongoDB is the MashupDB      Mongo Austin   February 15, 2011                                         WYNNN...
whoami
+
Two years ago...
x    No SQL?       x
MongoDB          x
Lets just skip to here already!x
One year ago
CTO of AmazonNoSQL   Smackdown!
Today
is it web scale?
x
How do we get to here?       xx
Enlightenment
Almost  ^    Two years with
Our journey
TweetCongress.org
“We the Tweeple of the UnitedStates, in order to form a moreperfect government, establishcommunication, and promotetranspa...
and aggregator                           ^The idea: A Twitter directory for the US Congress
When does NOSQL make sense?★ Your data is stored and retrieved mainly by primary key, without  complex joins.★ You have a ...
Key value stores Project Voldemort                           me ver  y cool Ringo Scalaris                        So      ...
Tokyo Cabinet
CouchDBApache CouchDB is a distributed, fault-tolerant andschema-free document-oriented database accessible viaa RESTful H...
SQL                                             CouchDB             Prede ned, explicit schema                            ...
SQL                                           CouchDB             Prede ned, explicit schema                           Dyn...
why I
Mongo speaks JSON
Document Storage (BSON)                     B is for Binary        { author: joe,            created: Date(03-28-2009),   ...
That looks like JSON
versatility
Runs like Hayes.Hits like Mays.
Introducing★ Built For Speedcan be very fast★ Dynamic Queries and Indexes★ Replication and Failover★ Sharding★ Map / Reduc...
MongoDB is great for ★ Websites                         Your own queryable API mirror ★ Caching ★ High volume, low value ★...
Not as great for ★ Highly transactional ★ Financial stuffs ★ Problems requiring SQL
Installation ★ mkdir -p /data/db ★ download pre-built for OSX and unzip to /usr/local/ ★ cp -R /usr/local/pathtomongo/bin ...
brew install mongodb
Contents of mongo/bin                 whats in the box? ★ mongod - The MongoDB server ★ mongo - the JavaScript interactive...
Some new terms
When I say   think database     database              Well that one isnt new...
Databases in MongoDB ★ Made up of multiple collections ★ Are created on-the- y when rst referenced
When I say    think collection    table
Collections in MongoDB ★ Schema-less    but typed! ★ For grouping documents into smaller query sets (speed) ★ Indexable by...
When I say   think document     record or row
The Ruby ecosystem
The Ruby driver
Queryingdb.collection.find({first_name: John}) # finds all Johnsdb.collection.find({first_name: /^wynn/i}) # regexdb.colle...
More  Querying  $in, $nin, $all, $ne, $gt, $gte, $lt, $lte, $size, $where  :fields (like :select in active record)  :limit...
Updating$inc, $set, $unset, $push, $pushAll, $addToSet,$pop, $pull, $pullAll, $rename, $bit
akfast w it hB re      Nune makerJohn
MongoMapper           from @jnunemaker ★ Mongo is not MySQL ★ DSL for modeling domain should also teach you Mongo ★ Now in...
Features★ Typecasting★ Callbacks (ActiveSupport Callbacks)★ Validations★ Connection and database can differ per document★ C...
Exampleclass User  include MongoMapper::Document  key :name, String, :required => true, :length => 5..100  key :email, Str...
QueriesUser.where(:age.gt   =>   27).sort(:age).allUser.where(:age.gt   =>   27).sort(:age.desc).allUser.where(:age.gt   =...
Scopesclass User  include MongoMapper::Document  # plain old vanilla scopes with fancy queries  scope :johns,   where(:nam...
Scopes# simple scopespp User.johns.firstpp User.bills.first# scope with argpp User.by_name(Frank).first# scope with two ar...
Mongoid
Mongomatic
Mongomaticrequire mongomatic class User < Mongomatic::Base  def validate    self.errors.add "name", "cant be empty" if sel...
MongomaticUser.empty?=> trueu = User.new(:name => "Ben")=> #<user:0x00000100d0cbf8 @doc="{"name"=">"Ben"}, @removed=false>...
MongoDB is open source
Mongo runs in the cloud
#mongodb
How can you help? ★ We need an awesome admin GUI ★ Port some plugins (might get easier with ActiveModel support   coming s...
Lessons learned in production                      the fine print ★ The laws of computing are still in effect            Up...
10gen
Get the backstory on
Questions?    wynn@hp.com     @pengwynn
MongoDB is the MashupDB
MongoDB is the MashupDB
MongoDB is the MashupDB
MongoDB is the MashupDB
MongoDB is the MashupDB
MongoDB is the MashupDB
Upcoming SlideShare
Loading in...5
×

MongoDB is the MashupDB

3,774

Published on

My slides from MongoAustin 2011

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

No Downloads
Views
Total Views
3,774
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
30
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Transcript of "MongoDB is the MashupDB"

  1. 1. Stash the Hash!MongoDB is the MashupDB Mongo Austin February 15, 2011 WYNNNETHERLAND
  2. 2. whoami
  3. 3. +
  4. 4. Two years ago...
  5. 5. x No SQL? x
  6. 6. MongoDB x
  7. 7. Lets just skip to here already!x
  8. 8. One year ago
  9. 9. CTO of AmazonNoSQL Smackdown!
  10. 10. Today
  11. 11. is it web scale?
  12. 12. x
  13. 13. How do we get to here? xx
  14. 14. Enlightenment
  15. 15. Almost ^ Two years with
  16. 16. Our journey
  17. 17. TweetCongress.org
  18. 18. “We the Tweeple of the UnitedStates, in order to form a moreperfect government, establishcommunication, and promotetransparency do hereby tweet theCongress of the United States ofAmerica.”
  19. 19. and aggregator ^The idea: A Twitter directory for the US Congress
  20. 20. When does NOSQL make sense?★ Your data is stored and retrieved mainly by primary key, without complex joins.★ You have a non-trivial amount of data, and the thought of managing lots of RDBMS shards and replication failure scenarios gives you the fear.http://www.metabrew.com/article/anti-rdbms-a-list-of-distributed-key-value-stores/
  21. 21. Key value stores Project Voldemort me ver y cool Ringo Scalaris So space Kai ojects in this Dynomite MemcacheDB pr ThruDB CouchDB Cassandra HBase Hypertable Redis Tokyo Cabinet/Tyrant Riak Neo4J
  22. 22. Tokyo Cabinet
  23. 23. CouchDBApache CouchDB is a distributed, fault-tolerant andschema-free document-oriented database accessible viaa RESTful HTTP/JSON API.http://couchdb.apache.org/
  24. 24. SQL CouchDB Prede ned, explicit schema Dynamic, implicit schema Collection of named documents with varying Uniform tables of data structure Normalized. Objects spread across tables. Denormalized. Docs usually self contained. Data Duplication reduced. often duplicated. Must know schema to read/write a complete Must know only document name object Dynamic queries of static schemas Static queries of dynamic schemas http://damienkatz.net/files/What is CouchDB.pdfhttp://damienkatz.net/files/What is CouchDB.pdf
  25. 25. SQL CouchDB Prede ned, explicit schema Dynamic, implicit schema Collection of named documents with varying Uniform tables of data structure Normalized. Objects spread across tables. Duplication reduced. TheDenormalized. Docs usually selfdetails devilsoften duplicated.contained. Data in the Must know schema to read/write a complete Must know only document name object Dynamic queries of static schemas Static queries of dynamic schemas http://damienkatz.net/files/What is CouchDB.pdfhttp://damienkatz.net/files/What is CouchDB.pdf
  26. 26. why I
  27. 27. Mongo speaks JSON
  28. 28. Document Storage (BSON) B is for Binary { author: joe, created: Date(03-28-2009), title: Yet another blog post, text: Here is the text..., tags: [ example, joe ], comments: [ { author: jim, comment: I disagree }, { author: nancy, comment: Good post } ] } Sure wish JSON did this...http://www.mongodb.org/display/DOCS/BSON
  29. 29. That looks like JSON
  30. 30. versatility
  31. 31. Runs like Hayes.Hits like Mays.
  32. 32. Introducing★ Built For Speedcan be very fast★ Dynamic Queries and Indexes★ Replication and Failover★ Sharding★ Map / Reduce★ Geospatial stuffs
  33. 33. MongoDB is great for ★ Websites Your own queryable API mirror ★ Caching ★ High volume, low value ★ High scalability stash the hash ★ Storage of program objects and JSON
  34. 34. Not as great for ★ Highly transactional ★ Financial stuffs ★ Problems requiring SQL
  35. 35. Installation ★ mkdir -p /data/db ★ download pre-built for OSX and unzip to /usr/local/ ★ cp -R /usr/local/pathtomongo/bin /usr/local/bin Ruby driver for MongoDB ★ gem install mongo Native C ★ gem install mongo_ext extensions ( go turbo! ) ★ gem install mongo_mapper
  36. 36. brew install mongodb
  37. 37. Contents of mongo/bin whats in the box? ★ mongod - The MongoDB server ★ mongo - the JavaScript interactive shell ★ mongoexport - export data as JSON or csv ★ mongoimport - As advertised was? ★ mongodump - Like mysqldump why? What did you think it ★ mongorestore - Restore from mongodump les ★ mongos - Auto-sharding module (getting better with every build)
  38. 38. Some new terms
  39. 39. When I say think database database Well that one isnt new...
  40. 40. Databases in MongoDB ★ Made up of multiple collections ★ Are created on-the- y when rst referenced
  41. 41. When I say think collection table
  42. 42. Collections in MongoDB ★ Schema-less but typed! ★ For grouping documents into smaller query sets (speed) ★ Indexable by one or more key ★ Are created on-the- y when rst referenced ★ Capped collections: Fixed size, older records dropped after limit reached
  43. 43. When I say think document record or row
  44. 44. The Ruby ecosystem
  45. 45. The Ruby driver
  46. 46. Queryingdb.collection.find({first_name: John}) # finds all Johnsdb.collection.find({first_name: /^wynn/i}) # regexdb.collection.find_first({_id:1}) # finds first with _id of 1db.collection.find({age: {$gte: 21}}) # finds possible drinkersdb.collection.find({author.first_name:John}) # subdocumentdb.collection.find({$where:this.age >= 6 && this.age <= 18})
  47. 47. More Querying $in, $nin, $all, $ne, $gt, $gte, $lt, $lte, $size, $where :fields (like :select in active record) :limit, :offset for pagination :sort ascending or descending [[foo, 1], [bar, -1]] count and group (uses map/reduce) db.collection.mapReduce(mapfunction,reducefunction[,options]);
  48. 48. Updating$inc, $set, $unset, $push, $pushAll, $addToSet,$pop, $pull, $pullAll, $rename, $bit
  49. 49. akfast w it hB re Nune makerJohn
  50. 50. MongoMapper from @jnunemaker ★ Mongo is not MySQL ★ DSL for modeling domain should also teach you Mongo ★ Now in version 0.8.6 I voted for "Nunemapper"
  51. 51. Features★ Typecasting★ Callbacks (ActiveSupport Callbacks)★ Validations★ Connection and database can differ per document★ Create and Update with single or multiple★ Delete and Destroy and _all counterparts Be careful. Ordering can be tricky.★ Find: id, ids, :all, : rst, :last★ Associations
  52. 52. Exampleclass User include MongoMapper::Document key :name, String, :required => true, :length => 5..100 key :email, String, :required => true, :index => true key :age, Integer, :numeric => true key :active, Boolean, :default => true one :address many :articlesend Included as module, not subclassedclass Address include MongoMapper::Document key :street, String key :city, String key :state, String, :length => 2 key :zip, Integer, :numeric => true, :length => 5end
  53. 53. QueriesUser.where(:age.gt => 27).sort(:age).allUser.where(:age.gt => 27).sort(:age.desc).allUser.where(:age.gt => 27).sort(:age).limit(1).allUser.where(:age.gt => 27).sort(:age).skip(1).limit(1).all
  54. 54. Scopesclass User include MongoMapper::Document # plain old vanilla scopes with fancy queries scope :johns, where(:name => John) # plain old vanilla scopes with hashes scope :bills, :name => Bill # dynamic scopes with parameters scope :by_name, lambda { |name| where(:name => name) } scope :by_ages, lambda { |low, high| where(:age.gte => low, :age.lte => high) } # Yep, even plain old methods work as long as they return a query def self.by_tag(tag) where(:tags => tag) end # You can even make a method that returns a scope def self.twenties; by_ages(20, 29) end key :name, String key :tags, Arrayend
  55. 55. Scopes# simple scopespp User.johns.firstpp User.bills.first# scope with argpp User.by_name(Frank).first# scope with two argspp User.by_ages(20, 29).all# chaining class methods on scopespp User.by_ages(20, 40).by_tag(ruby).all# scope made using method that returns scopepp User.twenties.all
  56. 56. Mongoid
  57. 57. Mongomatic
  58. 58. Mongomaticrequire mongomatic class User < Mongomatic::Base  def validate    self.errors.add "name", "cant be empty" if self["name"].blank?    self.errors.add "email", "cant be empty" if self["email"].blank?  endend # set the db for all models:Mongomatic.db = Mongo::Connection.new.db("mongomatic_test")# or you can set it for a specific model:User.db = Mongo::Connection.new.db("mongomatic_test_user") 
  59. 59. MongomaticUser.empty?=> trueu = User.new(:name => "Ben")=> #<user:0x00000100d0cbf8 @doc="{"name"=">"Ben"}, @removed=false>u.valid?=> falseu["email"] = "me@somewhere.com"u.valid?=> trueu.insert=> BSON::ObjectId(4c32834f0218236321000001) User.empty?=> false
  60. 60. MongoDB is open source
  61. 61. Mongo runs in the cloud
  62. 62. #mongodb
  63. 63. How can you help? ★ We need an awesome admin GUI ★ Port some plugins (might get easier with ActiveModel support coming soon) ★ Build something cool
  64. 64. Lessons learned in production the fine print ★ The laws of computing are still in effect Upserts FTW! ★ Indexes are important no matter what the salesman told ya about performance ★ Data modeling. Deep or Wide? The answer is yes! ★ MongoDB and MongoMapper are in active development Very responsive yet very volatile changes!
  65. 65. 10gen
  66. 66. Get the backstory on
  67. 67. Questions? wynn@hp.com @pengwynn
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×