MongoDB 101
Agenda• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing
Agenda• Aggregation• Joins and Transactions• Replica Sets• Sharding
Document JSON Example{"_id" :ObjectId("72f494c1c3df14726f1403b3"),"city" : "Vienna","zipcode" : "22180","pop" : 20795"surv...
Embedded Example{"name" : {"first" : "Joe","last" : "Schmoe"},"address" : {"street" : "123 Maple Avenue""city" : "Ashburn"...
Another Example{"title" : "MongoDB 101","author" : "John Ragan","content" : "My thoughts on MongoDB","comments" : [{ "name...
Documents• Analogous to a database row• Schema-free• Keys and Values– Strings– Value types• Special key: _id– unique
MongoDB and Relational CorollariesRelational Database MongoDBDatabase instance Mongo instanceDatabase(s) Database(s)Table(...
Easy to Get Started
Insertzip = {"city" : "Ashburn","loc" : [ -77.480612, 39.039918],"pop" : 19416,"state" : "VA”,"_id" : "20148"}db.census.in...
Insert• JSON converted to BSON• Must be less than 16 Mgs (in BSON)• Adds _id unless already specified
Find find() findOne()db.census.findOne({"_id": "22180"})
Projection> db.census.find({}, {"city" : 1, "state" : 1}){"_id" : 22180,"city" : "VIENNA","state" : "VA"}db.census.find({}...
Query Conditionals• $gt• $gte• $lt• $lte• $ne• $notdb.census.find({"_id" : {"$gte" : "70300", "$lte" :"70399"}})
In and Not In• $in• $nindb.census.find({"_id" : {"$in" : ["22180","70301", "22030"]}})
OR Queriesdb.census.find({"$or" :[{"_id" : {"$in" : ["22180", "90210"]}},{"city" : "ASHBURN"}]})
Regular Expressions• Perl Compatible Regular Expression (PCRE)db.census.find({"city" : /^ASHBU?/i})
Limits, Skips and Sortsdb.census.find({"city" : "CHICAGO"}).skip(3).limit(4).sort("zipcode" : -1})
Updateupdate{ <criteria>, <new doc> }db.census.update({_id : "22011"},{city : "BROADLANDS"})
Update - $set and $unsetdb.users.update({"name" : "joe"},{"$set" : {"favorite book" : "harry potter"}})db.users.update({"n...
Deletedb.census.remove( <criteria> )db.census.remove({city : "NORTH POLE"})db.census.remove()db.drop_collection("census")
Indexesdb.census.ensureIndex({"city" : 1})FAST:db.census.find().sort("city" : 1})SLOW:db.census.find().sort({"pop" : 1, "c...
Index Orderingdb.census.ensureIndex({"pop" : 1, "city" : 1})Fast or Slow?db.census.find().sort({"pop" : -1, "city" : 1})
Other Index Optionsdb.census.ensureIndex({"city" : 1},{"name" : "myIndex","unique" : true,"dropDups", true})
Explain• explain will return information– indexes used for the query (if any)– stats about timing– the number of documents...
Aggregation Framework• Largest and smallest cities in Virginia,California and Louisiana
MongoDB• Relational Databases are Dead
MongoDB• Relational Databases are Dead– Of course that is not true!– Right Tool for the Right Job
Why MongoDB?• Schema flexibility• Developer speed• Horizontal scalability
Developer Flexibility“An elephant should not always have to sit onyour data before you persist it”
Increasing Horizontal Scalability• No joins– Thus, no distributedjoins• No transactions– Thus, no distributedtransactions
Life Without Joins• Already denormalized or Reference Id’s• One to One relationships• One to Many relationships• Many to M...
Life Without Transactions• Document Level transaction boundaries• Nesting within documents• Two Phase commit
Update - $inc{"url" : "www.example.com","pageviews" : 52}db.analytics.update({"url" : "www.example.com"},{"$inc" : {"pagev...
Replica Sets• Primary-Secondary cluster– Automatic failover– Primary elected by cluster• One Primary, many Secondary– Othe...
Demo Replica Set Failover
Sharding• The process of splitting data up and storingdifferent portions of the data on differentmachines• Automatic vs. m...
Mongod Mongod MongodMongosClient
Sharding• Server types:– Shard• holds a subset of a collection’s data.– Single mongod server– Replica set– Mongos• router ...
Summary• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing
Summary• Aggregation• Joins and Transactions• Replica Sets• Sharding
Upcoming SlideShare
Loading in …5
×

Mongo db 101 dc group

1,288 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,288
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

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
  • Mongo db 101 dc group

    1. 1. MongoDB 101
    2. 2. Agenda• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing
    3. 3. Agenda• Aggregation• Joins and Transactions• Replica Sets• Sharding
    4. 4. Document JSON Example{"_id" :ObjectId("72f494c1c3df14726f1403b3"),"city" : "Vienna","zipcode" : "22180","pop" : 20795"surveyDate" : new Date()}
    5. 5. Embedded Example{"name" : {"first" : "Joe","last" : "Schmoe"},"address" : {"street" : "123 Maple Avenue""city" : "Ashburn""state" : "VA""zipcode" : "20148"}"age" : 45}
    6. 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. 7. Documents• Analogous to a database row• Schema-free• Keys and Values– Strings– Value types• Special key: _id– unique
    8. 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. 9. Easy to Get Started
    10. 10. Insertzip = {"city" : "Ashburn","loc" : [ -77.480612, 39.039918],"pop" : 19416,"state" : "VA”,"_id" : "20148"}db.census.insert(zip)
    11. 11. Insert• JSON converted to BSON• Must be less than 16 Mgs (in BSON)• Adds _id unless already specified
    12. 12. Find find() findOne()db.census.findOne({"_id": "22180"})
    13. 13. Projection> db.census.find({}, {"city" : 1, "state" : 1}){"_id" : 22180,"city" : "VIENNA","state" : "VA"}db.census.find({}, {"loc" : 0})
    14. 14. Query Conditionals• $gt• $gte• $lt• $lte• $ne• $notdb.census.find({"_id" : {"$gte" : "70300", "$lte" :"70399"}})
    15. 15. In and Not In• $in• $nindb.census.find({"_id" : {"$in" : ["22180","70301", "22030"]}})
    16. 16. OR Queriesdb.census.find({"$or" :[{"_id" : {"$in" : ["22180", "90210"]}},{"city" : "ASHBURN"}]})
    17. 17. Regular Expressions• Perl Compatible Regular Expression (PCRE)db.census.find({"city" : /^ASHBU?/i})
    18. 18. Limits, Skips and Sortsdb.census.find({"city" : "CHICAGO"}).skip(3).limit(4).sort("zipcode" : -1})
    19. 19. Updateupdate{ <criteria>, <new doc> }db.census.update({_id : "22011"},{city : "BROADLANDS"})
    20. 20. Update - $set and $unsetdb.users.update({"name" : "joe"},{"$set" : {"favorite book" : "harry potter"}})db.users.update({"name" : "joe"},{"$unset" : {"favorite book" : 1}})
    21. 21. Deletedb.census.remove( <criteria> )db.census.remove({city : "NORTH POLE"})db.census.remove()db.drop_collection("census")
    22. 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. 23. Index Orderingdb.census.ensureIndex({"pop" : 1, "city" : 1})Fast or Slow?db.census.find().sort({"pop" : -1, "city" : 1})
    24. 24. Other Index Optionsdb.census.ensureIndex({"city" : 1},{"name" : "myIndex","unique" : true,"dropDups", true})
    25. 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. 26. Aggregation Framework• Largest and smallest cities in Virginia,California and Louisiana
    27. 27. MongoDB• Relational Databases are Dead
    28. 28. MongoDB• Relational Databases are Dead– Of course that is not true!– Right Tool for the Right Job
    29. 29. Why MongoDB?• Schema flexibility• Developer speed• Horizontal scalability
    30. 30. Developer Flexibility“An elephant should not always have to sit onyour data before you persist it”
    31. 31. Increasing Horizontal Scalability• No joins– Thus, no distributedjoins• No transactions– Thus, no distributedtransactions
    32. 32. Life Without Joins• Already denormalized or Reference Id’s• One to One relationships• One to Many relationships• Many to Many references
    33. 33. Life Without Transactions• Document Level transaction boundaries• Nesting within documents• Two Phase commit
    34. 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. 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. 36. Demo Replica Set Failover
    37. 37. Sharding• The process of splitting data up and storingdifferent portions of the data on differentmachines• Automatic vs. manual• Chunks– Shard Key
    38. 38. Mongod Mongod MongodMongosClient
    39. 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. 40. Summary• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing
    41. 41. Summary• Aggregation• Joins and Transactions• Replica Sets• Sharding

    ×