Introduction to NoSQL db and mongoDB

Uploaded on

A short introduction to NoSQL databases and mongoDB for my colleagues.

A short introduction to NoSQL databases and mongoDB for my colleagues.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. BIO Senior SW Engineer @RAI Radiotelevisione italiana Rome, Italy QUIT IT APP @quititapp GET IN TOUCH!     @robertobelardo     Skype:  backslash451  
  • 2. Traditional RDBMS ACID (atomicity, Consistency, Isolation, Durability) Great for relation data widely adopted and well understood TRANSACTIONS JOINS
  • 3. but… •  •  •  •  joins are slow do not scale easily SQL is a pain in the ass -> ORM SCHema are not flexible - and -
  • 4. NOSQL DB ONLY •  •  •  •  ACID-LESS (NOT ALWAYS SWEET) Elastic scaling Big data Flexible data models KEY/VALUE STORES memcached db project voldemort redis dynamo DOCUMENT DB mongo db couch base GRAPH STORES neo4j orient db allegro virtuoso wide column stores big table hbase accumulo cassandra
  • 5. mongo features •  •  •  •  •  Schema-less bson document (binary json) full index support replication and high availability Easy to use
  • 6. Jargon RDBMS   mongoDB   Table   Collec=on   Row(s)   (JSON)  Document   Column   Field   Index   Index   Join   Embedding  &  Linking   Par==on   Shard   GROUP  BY   Aggrega=on  
  • 7. 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”! !}! ]}!
  • 8. CRUD querying INSERT  a  document  in  a  collec7on   >{name:”Sauron”, type:”bad-ass”, address:”Mordor”, eyes:1})! SELECT  all  documents  in  a  collec7on   >! SELECT  some  documents  in  a  collec7on   >{eyes:1})
 >{eyes:1, type:”bad-ass”})! UPDATE  a  document  in  a  collec7on   >{_id:10, name:”Sauron”, type:”good boy”})! >{type:”bad-ass”},{$inc:{eyes:1}},{multi:true})! DELETE  all  documents  in  a  collec7on   >! DELETE  some  documents  in  a  collec7on   >{eyes:1})!
  • 9. Advanced STUFF
  • 10. WRITE CONCERNS “Specifies  where  a  write  (insert)  opera3on  has  succeeded.”   “In  some  failure  cases,  write  opera3ons  issued  with  weak  write  concerns  may  not  persist.”   weak concern fast writes “With  stronger  write  concerns,  clients  wait  aAer  sending  a  write  opera3on  for  MongoDB  to   confirm  the  write  opera3on.”   errors ignored w:-1 It  does  not  acknowledge  write  op.  and  the  client  cannot  detect  failed  write  op.   unacknowledged w:0 It  does  not  acknowledge  write  op.  but  driver  aUempt  to  handle  network  errors.   acknowledged w:1 It  confirms  the  receipt  of  the  write  op.  allowing  clients  to  catch  errors.   journaled w:1,j:true It  confirms  the  receipt  of  the  write  op.  only  aWer  commiXng  the  data  to  journal.   replica acknowledged w:n (n>1) It  confirms  the  receipt  of  the  write  op.    aWer  acknowledging  primary  and  n-­‐1  secondaries.   DEFAULT  
  • 11. INDEXES Without  indexes,  mongoDB  must  scan  every  document  in  a  collec3on!   index types Default _ID single field compound index multikey index geospatial index text indexes hashed indexes properties unique indexes sparse indexes creating indexes >{phone-number:1}) ! ! >{name:1, phone-number:1})! ! >{name:1}, {unique:true})! >{nickname:1}, {sparse:true})! ! >{a:1},{unique:true,sparse:true})!
  • 12. REPLICAtion Replica3on  provides  redundancy  and  increases  data  availability.   Automatic failover: ELECTIONS
  • 13. SHARDING horizontal scaling the mongoway SHARDimmutable KEYs shard key is { range based sharding hash based sharding . 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 triggered automatically by a mongos if a chunk goes beyond max size (default 64mb) Balanc ing triggered automatically by a mongos if there is a chunk imbalance (8+ chunks)
  • 14. Aggre | gation MaP Reduce framework operators . filter . project . unwind . group . sort . limit > db.article.aggregate(! !{ $project: {author:1, tags: 1}},! !{ $unwind: “$tags”},! !{ $group: { !_id: “$tags”, ! ! ! ! !authors: { $addToSet: “$author”}}})! { “result”: !{“_id”: !{“_id”: !{“_id”: !{“_id”: !],! !“ok”:1! }! [! “art”, “authors”:[“bill”, “bob”]},! “sports”, “authors”:[“jane”, “bob”]},! “food”, “authors”:[“jane”, “bob”]},! “science”, “authors”:[“jane”, “bob”, “bill”]},!
  • 15. still a lot to cover! map reduce geo spatial GRID FS drivers etc….
  • 16. resources hBp://   hBp://   hBp://   hBp://   hBp://­‐as-­‐documents-­‐overview-­‐and-­‐intro-­‐ to-­‐mongodb   •  hBps://   •  hBps://   •  hBp://   •  •  •  •  •