Your SlideShare is downloading. ×
  • Like
Mongo db 101 dc group
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Mongo db 101 dc group

  • 855 views
Published

An introduction to MongoDB. You can exercise the examples against 10gen's zips.json file.

An introduction to MongoDB. You can exercise the examples against 10gen's zips.json file.

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

Views

Total Views
855
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
8
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Key value pairsData typesTalk about the idHow would you maps these key value pairs to a row in a table?
  • Documents can contain other documents
  • I can have arrays of arraysHow do I put this into a single row?How would you do this in 3rd normal form?Not just denormalized, multi-dimensional denormalizationIsn’t that wrong?This is all stored all in the same place on diskWould need joins to do this in RDBMSOur performance improvements
  • Analogous except where multi-dimensionalStrings, long numbers, dates, arrays and embedded objectsCan be any unique value for id, if desired
  • Go over relational side first
  • Start it up and shut it downPort 27017, 28017Ctrl-C
  • Project1 – include0 - exclude
  • AND is default for query criteria
  • $unset removes the key altogether
  • No parameter removes all documents Empty collectiondrop_collection removes the collection itself
  • Slow!
  • ZealotsA long lived proven technologyGives you incredible flexibility up front (but you may not need it)YAGNI
  • Question the ORM assumptionORM mappers like Hibernate or Ruby on Rails ActiveRecordFundamental mismatch OO and relational (handling sets of data, dealing with inheritance)Its not just about being to able to do something quick and dirty and paying for it later. You may never need the RDBMS flexibility. It is not just a poor man’s database
  • No need to join – everything is already all together. Joins are slow, how much slower distributed?Violates 3rd normal form. Yes. Create your schema based upon your application’s needs, not general flexibility- Cannot debit one record, and credit another in a single transactionOnly within a single document (which can have a sub-documents)- Distributed joins and two phase commits – slow!
  • Atomicity – transaction semantics
  • Hidden, slave delay, arbiters, priority levels
  • Shard Key - name

Transcript

  • 1. MongoDB 101
  • 2. Agenda• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing
  • 3. Agenda• Aggregation• Joins and Transactions• Replica Sets• Sharding
  • 4. Document JSON Example{"_id" :ObjectId("72f494c1c3df14726f1403b3"),"city" : "Vienna","zipcode" : "22180","pop" : 20795"surveyDate" : new Date()}
  • 5. Embedded Example{"name" : {"first" : "Joe","last" : "Schmoe"},"address" : {"street" : "123 Maple Avenue""city" : "Ashburn""state" : "VA""zipcode" : "20148"}"age" : 45}
  • 6. Another Example{"title" : "MongoDB 101","author" : "John Ragan","content" : "My thoughts on MongoDB","comments" : [{ "name" : "Jake the Troll","comment" : "My trollish comments",{ "name" : "Dwight Merriman","lastName" : ”Insightful comments from Dwight",},{ "name" : "Jake the Troll","comment" : "My even more trollish comments“,],"tags" : [ "mongodb", "101" ]}
  • 7. Documents• Analogous to a database row• Schema-free• Keys and Values– Strings– Value types• Special key: _id– unique
  • 8. MongoDB and Relational CorollariesRelational Database MongoDBDatabase instance Mongo instanceDatabase(s) Database(s)Table(s) Collection(s)Row(s) Document(s)Values Keys and Values
  • 9. Easy to Get Started
  • 10. Insertzip = {"city" : "Ashburn","loc" : [ -77.480612, 39.039918],"pop" : 19416,"state" : "VA”,"_id" : "20148"}db.census.insert(zip)
  • 11. Insert• JSON converted to BSON• Must be less than 16 Mgs (in BSON)• Adds _id unless already specified
  • 12. Find find() findOne()db.census.findOne({"_id": "22180"})
  • 13. Projection> db.census.find({}, {"city" : 1, "state" : 1}){"_id" : 22180,"city" : "VIENNA","state" : "VA"}db.census.find({}, {"loc" : 0})
  • 14. Query Conditionals• $gt• $gte• $lt• $lte• $ne• $notdb.census.find({"_id" : {"$gte" : "70300", "$lte" :"70399"}})
  • 15. In and Not In• $in• $nindb.census.find({"_id" : {"$in" : ["22180","70301", "22030"]}})
  • 16. OR Queriesdb.census.find({"$or" :[{"_id" : {"$in" : ["22180", "90210"]}},{"city" : "ASHBURN"}]})
  • 17. Regular Expressions• Perl Compatible Regular Expression (PCRE)db.census.find({"city" : /^ASHBU?/i})
  • 18. Limits, Skips and Sortsdb.census.find({"city" : "CHICAGO"}).skip(3).limit(4).sort("zipcode" : -1})
  • 19. Updateupdate{ <criteria>, <new doc> }db.census.update({_id : "22011"},{city : "BROADLANDS"})
  • 20. Update - $set and $unsetdb.users.update({"name" : "joe"},{"$set" : {"favorite book" : "harry potter"}})db.users.update({"name" : "joe"},{"$unset" : {"favorite book" : 1}})
  • 21. Deletedb.census.remove( <criteria> )db.census.remove({city : "NORTH POLE"})db.census.remove()db.drop_collection("census")
  • 22. Indexesdb.census.ensureIndex({"city" : 1})FAST:db.census.find().sort("city" : 1})SLOW:db.census.find().sort({"pop" : 1, "city" : 1})db.census.ensureIndex({"pop" : 1, "city" : 1})
  • 23. Index Orderingdb.census.ensureIndex({"pop" : 1, "city" : 1})Fast or Slow?db.census.find().sort({"pop" : -1, "city" : 1})
  • 24. Other Index Optionsdb.census.ensureIndex({"city" : 1},{"name" : "myIndex","unique" : true,"dropDups", true})
  • 25. Explain• explain will return information– indexes used for the query (if any)– stats about timing– the number of documents scanneddb.census.find({city:"CHICAGO"}).explain()
  • 26. Aggregation Framework• Largest and smallest cities in Virginia,California and Louisiana
  • 27. MongoDB• Relational Databases are Dead
  • 28. MongoDB• Relational Databases are Dead– Of course that is not true!– Right Tool for the Right Job
  • 29. Why MongoDB?• Schema flexibility• Developer speed• Horizontal scalability
  • 30. Developer Flexibility“An elephant should not always have to sit onyour data before you persist it”
  • 31. Increasing Horizontal Scalability• No joins– Thus, no distributedjoins• No transactions– Thus, no distributedtransactions
  • 32. Life Without Joins• Already denormalized or Reference Id’s• One to One relationships• One to Many relationships• Many to Many references
  • 33. Life Without Transactions• Document Level transaction boundaries• Nesting within documents• Two Phase commit
  • 34. Update - $inc{"url" : "www.example.com","pageviews" : 52}db.analytics.update({"url" : "www.example.com"},{"$inc" : {"pageviews" : 1}}){"url" : "www.example.com","pageviews" : 53}
  • 35. Replica Sets• Primary-Secondary cluster– Automatic failover– Primary elected by cluster• One Primary, many Secondary– Others• Fully automatic– It handles voting, etc.• 3 Node viable minimum
  • 36. Demo Replica Set Failover
  • 37. Sharding• The process of splitting data up and storingdifferent portions of the data on differentmachines• Automatic vs. manual• Chunks– Shard Key
  • 38. Mongod Mongod MongodMongosClient
  • 39. Sharding• Server types:– Shard• holds a subset of a collection’s data.– Single mongod server– Replica set– Mongos• router process and aggregates responses• Does not store anything– config server• Stores cluster configuration: which data is on which shard.• Start these in reverse
  • 40. Summary• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing
  • 41. Summary• Aggregation• Joins and Transactions• Replica Sets• Sharding