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

MongoDB 101

MongoDB 101

A crash course on MongoDB I gave to attendees at the JISC MRD Hackday organised by DevCSI.

C7f3b4756d808ef0e2548bd0965ac225?s=128

Nick Jackson

May 07, 2012
Tweet

More Decks by Nick Jackson

Other Decks in Technology

Transcript

  1. MongoDB 101

  2. I am... • Nick Jackson • Awesome Developer Dude •

    University of Lincoln • @jacksonj04
  3. MongoDB Is... • A NoSQL document database.

  4. Eh?

  5. NoSQL • Is not a specification (unlike SQL). • (Generally)

    doesn’t have schemas. • (Generally) works at web-scale. • (Generally) doesn’t do relational integrity.
  6. Document Databases • Store documents. • Don’t store key-value pairs.

    • Make building APIs and stuff really easy.
  7. Ye Olde SQL id name email 1 Nick nijackson@lincoln.ac.uk 2

    Joss jwinn@lincoln.ac.uk
  8. Ye Olde SQL id name email office building 1 Nick

    ... 3107 MHT 2 Joss ... 3015 MHT 3 Harry ... NULL MHT
  9. :-(

  10. Documents: Awesome! { name: ‘Nick’, email: ‘nijackson@lincoln.ac.uk’, location: { office:

    ‘3105’, building: ‘MHT’ } }
  11. Documents: Awesome! { name: ‘Harry’, email: ‘hnewton@lincoln.ac.uk’ }

  12. Documents: Awesome! { name: ‘Joss’, email: ‘jwinn@lincoln.ac.uk’, location: { office:

    ‘3105’, building: ‘MHT’ }, likes: { music: [‘Folk’, ‘Hip-Hop’], drink: [‘Coffee’, ‘Ale’] } }
  13. Servers Are Easy

  14. On Its Own Mongo App

  15. Replicated Mongo 1 Mongo 2 Mongo 3 App

  16. Sharded App Router 1 Router 2 Mongo S1 Mongo S2

    Mongo S3
  17. http://mongodb.org

  18. Inserts <3 JSON > db.people.save({name:'Nick'}) > db.people.save({name:'Joss'}) > db.people.save({name:'Harry'})

  19. Query Be Simple... > db.people.find() { "_id" : ObjectId("4fa...103"), "name"

    : "Nick" } { "_id" : ObjectId("4fa...104"), "name" : "Joss" } { "_id" : ObjectId("4fa...105"), "name" : "Harry" }
  20. Query Be Simple... > db.people.find({name:'Nick'}) { "_id" : ObjectId("4fa...103"), "name"

    : "Nick" }
  21. Query Be Quick... > db.people.ensureIndex({name:1})

  22. Updates are easy > db.people.update({name:'Nick'},{name:'Nick',likes: ['coffee']}) > db.people.find({name:'Nick'}) { "_id"

    : ObjectId("4fa...103"), "name" : "Nick", "likes" : [ "coffee" ] }
  23. Queries are powerful > db.people.update({name:'Joss'},{name:'Joss',likes: ['coffee','folk music']}) > db.people.find({likes:'coffee'}) {

    "_id" : ObjectId("4fa...103"), "name" : "Nick", "likes" : [ "coffee" ] } { "_id" : ObjectId("4fa...104"), "name" : "Joss", "likes" : [ "coffee", "folk music" ] }
  24. Deletes are also easy > db.people.remove({name:'Nick'}) > db.people.find() { "_id"

    : ObjectId("4fa...104"), "name" : "Joss", "likes" : [ "coffee", "folk music" ] } { "_id" : ObjectId("4fa...105"), "name" : "Harry" }
  25. Cool Things! • Geospatial indexes. db.places.find({loc:{$near:[-2, 53]}}) • JavaScript in

    the Mongo shell. • Map/Reduce operations. • Can be used as a filesystem.
  26. Downsides It has a few

  27. It’s not ACID • Set of atomic operators, but no

    things like transactions. • No enforced consistency. At all. • No locking, so updates can collide and be lost. • Disk writes are (usually) deferred, so data can be lost in failures.
  28. It’s not ‘Enterprise’ • Your DBAs will find it new

    and scary. • You need to un-learn a lot of the SQL mindset. • It’s not seen as ‘proven’, but this is generally rubbish.
  29. Some MongoDB Users • Craigslist • MTV • SourceForge •

    Disney • National Archives • HM Government • The Guardian • New York Times • bit.ly • GitHub • Foursquare • http://lncn.eu/fhx5