Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Schema Design by Example         Kevin Hanson       Solutions Architect         @hungarianhc       kevin@10gen.com        ...
Agenda• MongoDB Data Model• Blog Posts & Comments• Geospatial Check-Ins• Food For Thought
MongoDB Data Model:  Rich Documents {     title: ‘Who Needs Rows?’,     reasons: [       { name: ‘scalability’,         de...
Embedded Documents   { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),     author : "roger",     date : "Sat Jul 24 2010 19:47...
Parallels
ParallelsRDBMS               MongoDBTable               CollectionRow                 DocumentColumn              FieldInd...
Relational                      Category                  • Name                  • Url                           Article ...
MongoDB                            Article                     • Name                     • Slug                     • Pub...
Blog Posts and Comments
How Should the Documents Look?What Are We Going to Do with the            Data?       To embed or to link...       That is...
1) Fully Embedded{    blog-title: ‘Commuting to Work’,    blog-text: [         ‘This section is about airplanes’,         ...
1) Fully Embedded
1) Fully EmbeddedPros• Can query the comments or the blog for results• Cleanly encapsulated
1) Fully EmbeddedPros• Can query the comments or the blog for results• Cleanly encapsulatedCons• What if we get too many c...
2) Separating Blog & Comments{                             {  _id:                          _id:ObjectId("4c4ba5c0672c685 ...
2) Separating Blog & Comments
2) Separating Blog & CommentsPros• Blog Post Size Stays Constant• Can Search Sets of Comments
2) Separating Blog & CommentsPros• Blog Post Size Stays Constant• Can Search Sets of CommentsCons• Too Many Comments? (sam...
3) Each Comment Gets Own Doc      {          blog-title: ‘Commuting to Work’,          blog-text: [             ‘This sect...
3) Each Comment Gets Own Doc
3) Each Comment Gets Own DocPros• Can Query Individual Comments• Never Need to Worry About Doc Size
3) Each Comment Gets Own DocPros• Can Query Individual Comments• Never Need to Worry About Doc SizeCons• Many Documents• S...
Managing ArraysPushing to an Array Infinitely...• Document Will Grow Larger than AllocatedSpace• Document May Increase Max ...
Geospatial Check-Ins
We Need 3 Things
We Need 3 ThingsPlaces    Check-Ins   Users
Places                    Q: Current location                       A: Places near                          locationUser G...
Inserting a Placevar p = { name: “10gen HQ”,      address: “578 Broadway, 7thFloor”,      city: “New York”,      zip: “100...
Tags, Geo Coordinates, and Tips       { name: “10gen HQ”,       address: “578 Broadway, 7th Floor”,       city: “New York”...
Updating Tipsdb.places.update({name:"10gen HQ"},     {$push :{tips:              {user:"nosh", time:3/15/2012,            ...
Querying Places• Creating Indexes  ★db.places.ensureIndex({tags:1})  ★db.places.ensureIndex({name:1})  ★db.places.ensureIn...
User Check-InsRecord User Check-Ins    Users                 Stats           Stats  Check-Ins              Users
Usersuser1 = {    name: “Kevin Hanson”    e-mail: “kevin@10gen.com”,    check-ins:[4b97e62bf1d8c7152c9ccb74,5a20e62bf1d8c7...
Check-Inscheckin = {    place: “10gen HQ”,    ts: 9/20/2010 10:12:00,    userId: <object id of user>}Every Check-In is Two...
Simple Statsdb.checkins.find({place: “10gen HQ”)db.checkins.find({place: “10gen HQ”})	 	  	  	  .sort({ts:-1}).limit(10)db.c...
Stats w/ MapReducemapFunc = function() {emit(this.place, 1);}reduceFunc = function(key, values) {returnArray.sum(values);}...
Food For Thought
Data How the App Wants ItThink About How the Application Wantsthe Data, Not How it is most “Normalized”Example: Our Busine...
More info at http://www.mongodb.org/               Kevin Hanson        Solutions Architect, 10gen          twitter: @hunga...
Schema Design by Example ~ MongoSF 2012
Upcoming SlideShare
Loading in …5
×

Schema Design by Example ~ MongoSF 2012

8,610 views

Published on

This is my presentation about schema design in MongoDB. I gave this talk at MongoSF 2012.

Published in: Technology
  • Be the first to comment

Schema Design by Example ~ MongoSF 2012

  1. 1. Schema Design by Example Kevin Hanson Solutions Architect @hungarianhc kevin@10gen.com 1
  2. 2. Agenda• MongoDB Data Model• Blog Posts & Comments• Geospatial Check-Ins• Food For Thought
  3. 3. MongoDB Data Model: Rich Documents { title: ‘Who Needs Rows?’, reasons: [ { name: ‘scalability’, desc: ‘no more joins!’ }, { name: ‘human readable’, desc: ‘ah this is nice...’ } ], model: { relational: false, awesome: true } }
  4. 4. Embedded Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ], comments : [ { author : "Fred", date : "Sat Jul 24 2010 20:51:03 GMT-0700 (PDT)", text : "Best Movie Ever" } ]}
  5. 5. Parallels
  6. 6. ParallelsRDBMS MongoDBTable CollectionRow DocumentColumn FieldIndex IndexJoin Embedding & LinkingSchema Object
  7. 7. Relational Category • Name • Url Article User • Name Tag• Name • Slug • Name• Email Address • Publish date • Url • Text Comment • Comment • Date • Author
  8. 8. MongoDB Article • Name • Slug • Publish date User • Text• Name • Author• Email Address Comment[] • Comment • Date • Author Tag[] • Value Category[] • Value
  9. 9. Blog Posts and Comments
  10. 10. How Should the Documents Look?What Are We Going to Do with the Data? To embed or to link... That is the question!
  11. 11. 1) Fully Embedded{ blog-title: ‘Commuting to Work’, blog-text: [ ‘This section is about airplanes’, ‘this section is about trains’ ], comments: [ { author: ‘Kevin Hanson’, comment: ‘dude, what about driving?’ }, { author: ‘John Smith’, comment: ‘this blog is aWful!!11!!!!’ } ],}
  12. 12. 1) Fully Embedded
  13. 13. 1) Fully EmbeddedPros• Can query the comments or the blog for results• Cleanly encapsulated
  14. 14. 1) Fully EmbeddedPros• Can query the comments or the blog for results• Cleanly encapsulatedCons• What if we get too many comments? (16MBMongoDB doc size)• What if we want our results to be comments, notblog posts?
  15. 15. 2) Separating Blog & Comments{ { _id: _id:ObjectId("4c4ba5c0672c685 ObjectId("4c4ba5c0672c685e5ee5e8aabf3") 8aabf4") comment-ref: blog-ref:ObjectId("4c4ba5c0672c685 ObjectId("4c4ba5c0672c685e5ee5e8aabf4") 8aabf3") blog-title: ‘Commuting to comments: [Work’, { author: ‘Kevin Hanson’, blog-text: [ comment: ‘dude, what about ‘This section is about driving?’ },airplanes’, { author: ‘John Smith’, ‘this section is about comment: ‘this blog istrains’ aWful!!11!!!!’ } ] ],} }
  16. 16. 2) Separating Blog & Comments
  17. 17. 2) Separating Blog & CommentsPros• Blog Post Size Stays Constant• Can Search Sets of Comments
  18. 18. 2) Separating Blog & CommentsPros• Blog Post Size Stays Constant• Can Search Sets of CommentsCons• Too Many Comments? (same problem)• Managing Document Links
  19. 19. 3) Each Comment Gets Own Doc { blog-title: ‘Commuting to Work’, blog-text: [ ‘This section is about airplanes’, ‘this section is about trains’] } { commenter: ‘Kevin Hanson’, comment: ‘dude, what about driving?’ } { commenter: ‘John Smith’, comment: ‘this blog is aWful!!11!!!!’ }
  20. 20. 3) Each Comment Gets Own Doc
  21. 21. 3) Each Comment Gets Own DocPros• Can Query Individual Comments• Never Need to Worry About Doc Size
  22. 22. 3) Each Comment Gets Own DocPros• Can Query Individual Comments• Never Need to Worry About Doc SizeCons• Many Documents• Standard Use Cases Become Complicated
  23. 23. Managing ArraysPushing to an Array Infinitely...• Document Will Grow Larger than AllocatedSpace• Document May Increase Max Doc Size of16MBCan this be avoided??• Yes!• A Hybrid of Linking and Embedding
  24. 24. Geospatial Check-Ins
  25. 25. We Need 3 Things
  26. 26. We Need 3 ThingsPlaces Check-Ins Users
  27. 27. Places Q: Current location A: Places near locationUser Generated Content Places
  28. 28. Inserting a Placevar p = { name: “10gen HQ”, address: “578 Broadway, 7thFloor”, city: “New York”, zip: “10012”}> db.places.save(p)
  29. 29. Tags, Geo Coordinates, and Tips { name: “10gen HQ”, address: “578 Broadway, 7th Floor”, city: “New York”, zip: “10012”, tags: [“MongoDB”, “business”], latlong: [“40.0”, “72.0”], tips: [{user: “kevin”, time:“3/15/2012”,tip: “Make sure to stop byfor office hours!”}],}
  30. 30. Updating Tipsdb.places.update({name:"10gen HQ"}, {$push :{tips: {user:"nosh", time:3/15/2012, tip:"stop by foroffice hours on Wednesdays from 4-6"}}}}
  31. 31. Querying Places• Creating Indexes ★db.places.ensureIndex({tags:1}) ★db.places.ensureIndex({name:1}) ★db.places.ensureIndex({latlong:”2d”} )• Finding Places ★db.places.find({latlong:{$near: [40,70]}})• Regular Expressions ★db.places.find({name: / ^typeaheadstring/)
  32. 32. User Check-InsRecord User Check-Ins Users Stats Stats Check-Ins Users
  33. 33. Usersuser1 = { name: “Kevin Hanson” e-mail: “kevin@10gen.com”, check-ins:[4b97e62bf1d8c7152c9ccb74,5a20e62bf1d8c736ab]}checkins [] = ObjectId reference to Check-Ins Collection
  34. 34. Check-Inscheckin = { place: “10gen HQ”, ts: 9/20/2010 10:12:00, userId: <object id of user>}Every Check-In is Two Operations• Insert a Check-In Object (check-inscollection)• Update ($push) user object with check-in
  35. 35. Simple Statsdb.checkins.find({place: “10gen HQ”)db.checkins.find({place: “10gen HQ”}) .sort({ts:-1}).limit(10)db.checkins.find({place: “10gen HQ”, ts: {$gt: midnight}}).count()
  36. 36. Stats w/ MapReducemapFunc = function() {emit(this.place, 1);}reduceFunc = function(key, values) {returnArray.sum(values);}res =db.checkins.mapReduce(mapFunc,reduceFunc, {query: {timestamp: {$gt:nowminus3hrs}}})res = [{_id:”10gen HQ”, value: 17}, ….., ….] ... or try using the new aggregation framework! Chris Westin @ 1:50PM
  37. 37. Food For Thought
  38. 38. Data How the App Wants ItThink About How the Application Wantsthe Data, Not How it is most “Normalized”Example: Our Business Cards
  39. 39. More info at http://www.mongodb.org/ Kevin Hanson Solutions Architect, 10gen twitter: @hungarianhc kevin@10gen.com Facebook | Twitter |http://bit.ly/ @mongodb http://linkd.in/joinmongo LinkedInmongofb

×