Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB IndyRB

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

MongoDB IndyRB

Avatar for John Nunemaker

John Nunemaker PRO

August 19, 2009
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. INTRO • Built For Speed • Document/Collection Oriented • Dynamic

    Queries and Indexes • Replication and Failover
  2. GREAT FOR • Websites • Caching • High volume, low

    value • High scalability • Storage of program objects and json
  3. NOT AS GREAT FOR • Highly transactional • Ad-hoc business

    intelligence • Problems requiring SQL
  4. INSTALLATION • mkdir -p /data/db • download, unzip, mongod run

    • http://video.railstips.org/mongomapper-demo/
  5. COLLECTION • Think table, but with no schema • For

    grouping documents into smaller query sets (speed) • Each top level entity in your app would have its own collection (users, articles, etc.) • Indexable by one or more key
  6. DOCUMENT • Stored in a collection, think record or row

    • Can have _id key that works like primary keys in MySQL • Three options for relationships: embedded document, foreign keys, or db reference
  7. QUERYING • db.collection. fi nd({‘ fi rst_name’: ‘John’}) # fi

    nds all Johns • db.collection. fi nd({‘ fi rst_name’: /^J/}) # regex • db.collection. fi nd_ fi rst({‘_id’:1}) # fi nds fi rst with _id of 1 • db.collection. fi nd({‘age’: {‘$gt’: 21}}) # fi nds possible drinkers • db.collection. fi nd({‘author. fi rst_name’:‘John’}) # subdocument • db.collection. fi nd({$where:‘this.age >= 6 && this.age <= 18’})
  8. MORE QUERYING • $in, $nin, $all, $ne, $gt, $gte, $lt,

    $lte, $size, $where • : fi elds (like :select in active record) • :limit, :offset for pagination • :sort ascending or descending [[‘foo’, 1], [‘bar’, -1]] • count and group (uses map/reduce)
  9. HOW DO YOU USE IT WITH RUBY • mongo-ruby-driver http://github.com/mongodb/mongo-ruby-

    driver • active record adapter http://github.com/mongodb/ activerecord-mongo-adapter • mongorecord http://github.com/mongodb/mongo- activerecord-ruby
  10. NUNEMAPPER (MONGOMAPPER) • Mongo is not MySQL • DSL for

    modeling domain should also teach you Mongo • It sounded fun • Major appreciation for ORM’s
  11. FEATURES • Typecasting • Callbacks (ActiveSupport Callbacks) • Validations (using

    my fork of validatable) • Connection and database can differ per document • Create and Update with single or multiple • Delete and Destroy and _all counterparts • Find: id, ids, :all, : fi rst, :last • Associations
  12. EXAMPLE class 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 key :address, Address many :articles end class Address include MongoMapper::EmbeddedDocument key :street, String key :city, String key :state, String, :length => 2 key :zip, Integer, :numeric => true, :length => 5 end
  13. RANDOM AWESOMENESS • Capped collections (think memcache, actually used for

    replication) • Upserts db.collection.update({‘_id’:1}, {‘$inc’:{‘views’:1}}) • Multikeys (think tagging and full text search) • GridFS and auto-sharding