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

Introduction to mongoDB

Introduction to mongoDB

A quick introduction to mongoDB and NoSQL databases

Roberto Belardo

January 20, 2014
Tweet

More Decks by Roberto Belardo

Other Decks in Programming

Transcript

  1. introduction to @robertobelardo MongoDB f r o m r d

    b m s t o n o s q l d a t a b a s e s
  2. Senior SW Engineer @RAI Radiotelevisione italiana Rome, Italy GET IN

    TOUCH! [email protected]     @robertobelardo     robertobelardo.wordpress.com     Skype:  backslash451   QUIT IT APP @quititapp BIO
  3. Traditional RDBMS ACID (atomicity, Consistency, Isolation, Durability) Great for relation

    data widely adopted and well understood TRANSACTIONS JOINS
  4. but… •  joins are slow •  do not scale easily

    •  SQL is a pain in the ass -> ORM •  SCHema are not flexible - and -
  5. NOSQL DB •  ACID-LESS (NOT ALWAYS SWEET) •  Elastic scaling

    •  Big data •  Flexible data models ONLY KEY/VALUE STORES DOCUMENT DB GRAPH STORES wide column stores memcached db project voldemort redis dynamo mongo db couch base neo4j orient db allegro virtuoso big table hbase accumulo cassandra
  6. MONGODB “MongoDB (from humongous) is a scalable, high-performance, open source,

    schema-free, document-oriented database” (mongodb.org)
  7. mongo features •  Schema-less •  bson document (binary json) • 

    full index support •  replication and high availability •  Easy to use
  8. Jargon RDBMS   mongoDB   Table   Collec=on   Row(s)

      (JSON)  Document   Column   Field   Index   Index   Join   Embedding  &  Linking   Par==on   Shard   GROUP  BY   Aggrega=on  
  9. Data SNIPPET {“_id”: !ObjectId(“4dfa6baa9c65dae09a4bbda5”),! “title”: “Programming in Scala”,! “author”: [!

    !{! ! !“first_name”: ! !“Martin”,! ! !“last_name”: ! !“Odersky”,! ! !“nationality”: !“DE”,! ! !“year_of_birth”: !1958! !},! !{! ! !“first_name”: ! !“Lex”,! ! !“last_name”: ! !“Spoon”! !},! !{! ! !“first_name”: ! !“Bill”,! ! !“last_name”: ! !“Venners”! !}! ]}!
  10. CRUD querying INSERT  a  document  in  a  collec7on   >

    db.foo.insert({name:”Sauron”, type:”bad-ass”, address:”Mordor”, eyes:1})! SELECT  all  documents  in  a  collec7on   > db.foo.find()! SELECT  some  documents  in  a  collec7on   > db.foo.find({eyes:1})
 > db.foo.find({eyes:1, type:”bad-ass”})! UPDATE  a  document  in  a  collec7on   > db.foo.save({_id:10, name:”Sauron”, type:”good boy”})! > db.foo.update({type:”bad-ass”},{$inc:{eyes:1}},{multi:true})! DELETE  all  documents  in  a  collec7on   > db.foo.remove()! DELETE  some  documents  in  a  collec7on   > db.foo.remove({eyes:1})!
  11. WRITE CONCERNS “Specifies  where  a  write  (insert)  opera3on  has  succeeded.”

      weak concern fast writes “In  some  failure  cases,  write  opera3ons  issued  with  weak  write  concerns  may  not  persist.”   “With  stronger  write  concerns,  clients  wait  aAer  sending  a  write  opera3on  for  MongoDB  to   confirm  the  write  opera3on.”   errors ignored w:-1 unacknowledged w:0 acknowledged w:1 journaled w:1,j:true replica acknowledged w:n (n>1) It  does  not  acknowledge  write  op.  and  the  client  cannot  detect  failed  write  op.   It  does  not  acknowledge  write  op.  but  driver  aUempt  to  handle  network  errors.   It  confirms  the  receipt  of  the  write  op.  allowing  clients  to  catch  errors.   DEFAULT   It  confirms  the  receipt  of  the  write  op.  only  aWer  commiXng  the  data  to  journal.   It  confirms  the  receipt  of  the  write  op.    aWer  acknowledging  primary  and  n-­‐1  secondaries.  
  12. INDEXES Without  indexes,  mongoDB  must  scan  every  document  in  a

     collec3on!   Default _ID single field compound index multikey index geospatial index text indexes hashed indexes unique indexes sparse indexes properties index types > db.foo.ensureIndex({phone-number:1}) ! ! > db.foo.ensureIndex({name:1, phone-number:1})! ! > db.foo.ensureIndex({name:1}, {unique:true})! > db.foo.ensureIndex({nickname:1}, {sparse:true})! ! > db.foo.ensureIndex({a:1},{unique:true,sparse:true})! creating indexes
  13. SHARDING horizontal scaling the mongoway SHARD KEYs range based sharding

    hash based sharding { shard key is immutable . easily divisible among shards . high degree of randomness . targets a single shard . compound shard key > db.runCommand({ !shardcollection:”test.users”, ! ! ! ! !key: { email:1}})! split--ting Balanc ing triggered automatically by a mongos if a chunk goes beyond max size (default 64mb) triggered automatically by a mongos if there is a chunk imbalance (8+ chunks)
  14. MaP Reduce Aggre | gation > db.article.aggregate(! !{ $project: {author:1,

    tags: 1}},! !{ $unwind: “$tags”},! !{ $group: { !_id: “$tags”, ! ! ! ! !authors: { $addToSet: “$author”}}})! . filter . project . unwind . group . sort . limit operators { “result”: [! !{“_id”: “art”, “authors”:[“bill”, “bob”]},! !{“_id”: “sports”, “authors”:[“jane”, “bob”]},! !{“_id”: “food”, “authors”:[“jane”, “bob”]},! !{“_id”: “science”, “authors”:[“jane”, “bob”, “bill”]},! !],! !“ok”:1! }! framework
  15. resources •  hBp://www.mongodb.org/   •  hBp://www.mongodb.com/learn/nosql   •  hBp://www.mongodb.org/about/introduc7on/  

    •  hBp://try.mongodb.org/   •  hBp://www.slideshare.net/spacemonkeylabs/data-­‐as-­‐documents-­‐overview-­‐and-­‐intro-­‐ to-­‐mongodb   •  hBps://speakerdeck.com/backslash451   •  hBps://educa7on.mongodb.com/   •  hBp://lmgQy.com/?q=mongoDB