mongoosejs maintainer, 10genAaron Heckmann@mongodbSchema Design
http://mongoosejs.com
Agenda•  Working with documents•  Evolving a Schema•  Queries and Indexes•  Common Patterns
TerminologyRDBMS MongoDBDatabase ➜ DatabaseTable ➜ CollectionRow ➜ DocumentIndex ➜ IndexJoin ➜ Embedded DocumentForeign Ke...
Working with Documents
M O NXRaysCheckupsAllergiesModeling Data
DocumentsProvide flexibility andperformance
User·Name·Email addressCategory·Name·URLComment·Comment·Date·AuthorArticle·Name·Slug·Publish date·TextTag·Name·URLNormaliz...
User·Name·Email addressArticle·Name·Slug·Publish date·Text·AuthorComment[]·Comment·Date·AuthorTag[]·ValueCategory[]·ValueD...
Relational Schema DesignFocus on data storage
Document Schema DesignFocus on data use
Schema Design Considerations•  How do we manipulate the data?–  Dynamic Ad-Hoc Queries–  Atomic Updates•  What are the acc...
Data Manipulation•  Query Selectors–  Scalar: $ne,$mod,$exists,$type,$lt,$lte,$gt,$gte–  Vector: $in,$nin,$all,$size•  Ato...
Data Access•  Flexible Schemas•  Ability to embed complex data structures•  Secondary Indexes•  Multi-Key Indexes•  Aggreg...
Getting Started
Library Management Application•  Patrons•  Books•  Authors•  Publishers
An ExampleOne to One Relations
patron = {_id: "joe",name: "Joe Bookreader”}address = {patron_id = "joe",street: "123 Fake St. ",city: "Faketon",state: "M...
One to One Relations•  Mostly the same as the relational approach•  Generally good idea to embed“contains”relationships•  ...
An ExampleOne To Many Relations
Publishers and Books•  Publishers put out many books•  Books have one publisher
MongoDB: The Definitive Guide,By Kristina Chodorow and Mike DirolfPublished: 9/24/2010Pages: 216Language: EnglishPublisher...
book = {title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("20...
publisher = {name: "O’Reilly Media",founded: "1980",location: "CA"}book = {title: "MongoDB: The Definitive Guide",authors:...
publisher = {_id: "oreilly",name: "O’Reilly Media",founded: "1980",location: "CA"}book = {title: "MongoDB: The Definitive ...
publisher = {name: "O’Reilly Media",founded: "1980",location: "CA"books: [ "123456789", ... ]}book = {_id: "123456789",tit...
Where Do You Put the Foreign Key?•  Array of books inside of publisher–  Makes sense when many means a handful of items–  ...
Another ExampleOne to Many Relations
Books and Patrons•  Book can be checked out by one Patron at a time•  Patrons can check out many books (but not 1000’s)
patron = {_id: "joe",name: "Joe Bookreader",join_date: ISODate("2011-10-15"),address: { ... }}book = {_id: "123456789",tit...
patron = {_id: "joe",name: "Joe Bookreader",join_date: ISODate("2011-10-15"),address: { ... },checked_out: [{ _id: "123456...
DenormalizationProvides data locality
patron = {_id: "joe",name: "Joe Bookreader",join_date: ISODate("2011-10-15"),address: { ... },checked_out: [{ _id: "123456...
Referencing vs. Embedding•  Embedding is a bit like pre-joined data•  Document-level ops are“easier”•  Embed when themanyo...
An ExampleSingle Table Inheritance
book = {title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("20...
An ExampleMany to Many Relations
book = {title: "MongoDB: The Definitive Guide",authors: [{ _id: "kchodorow", name: "K-Awesome" },{ _id: "mdirolf", name: "...
book = {_id: 123456789,title: "MongoDB: The Definitive Guide",authors = [ "kchodorow", "mdirolf" ],published_date: ISODate...
An ExampleQueues
book = {_id: 123456789,title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_da...
mongoosejs maintainer, 10genAaron Heckmann@mongodbThank You
Upcoming SlideShare
Loading in …5
×

MongoDB Schema Design

1,119 views
1,024 views

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,119
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

MongoDB Schema Design

  1. 1. mongoosejs maintainer, 10genAaron Heckmann@mongodbSchema Design
  2. 2. http://mongoosejs.com
  3. 3. Agenda•  Working with documents•  Evolving a Schema•  Queries and Indexes•  Common Patterns
  4. 4. TerminologyRDBMS MongoDBDatabase ➜ DatabaseTable ➜ CollectionRow ➜ DocumentIndex ➜ IndexJoin ➜ Embedded DocumentForeign Key ➜ Reference
  5. 5. Working with Documents
  6. 6. M O NXRaysCheckupsAllergiesModeling Data
  7. 7. DocumentsProvide flexibility andperformance
  8. 8. User·Name·Email addressCategory·Name·URLComment·Comment·Date·AuthorArticle·Name·Slug·Publish date·TextTag·Name·URLNormalized Data
  9. 9. User·Name·Email addressArticle·Name·Slug·Publish date·Text·AuthorComment[]·Comment·Date·AuthorTag[]·ValueCategory[]·ValueDe-Normalized (embedded) Data
  10. 10. Relational Schema DesignFocus on data storage
  11. 11. Document Schema DesignFocus on data use
  12. 12. Schema Design Considerations•  How do we manipulate the data?–  Dynamic Ad-Hoc Queries–  Atomic Updates•  What are the access patterns of the application?–  Read/Write Ratio–  Types of Queries / Updates–  Data life-cycle and growth rate
  13. 13. Data Manipulation•  Query Selectors–  Scalar: $ne,$mod,$exists,$type,$lt,$lte,$gt,$gte–  Vector: $in,$nin,$all,$size•  Atomic Update Operators–  Scalar: $inc,$set,$unset–  Vector: $push,$pop,$pull,$pushAll,$pullAll,$addToSet
  14. 14. Data Access•  Flexible Schemas•  Ability to embed complex data structures•  Secondary Indexes•  Multi-Key Indexes•  Aggregation Framework–  $project,$match,$limit,$skip,$sort,$group,$unwind•  No Joins
  15. 15. Getting Started
  16. 16. Library Management Application•  Patrons•  Books•  Authors•  Publishers
  17. 17. An ExampleOne to One Relations
  18. 18. patron = {_id: "joe",name: "Joe Bookreader”}address = {patron_id = "joe",street: "123 Fake St. ",city: "Faketon",state: "MA",zip: 12345}Modeling Patronspatron = {_id: "joe",name: "Joe Bookreader",address: {street: "123 Fake St. ",city: "Faketon",state: "MA",zip: 12345}}
  19. 19. One to One Relations•  Mostly the same as the relational approach•  Generally good idea to embed“contains”relationships•  Document model provides a holisticrepresentation of objects
  20. 20. An ExampleOne To Many Relations
  21. 21. Publishers and Books•  Publishers put out many books•  Books have one publisher
  22. 22. MongoDB: The Definitive Guide,By Kristina Chodorow and Mike DirolfPublished: 9/24/2010Pages: 216Language: EnglishPublisher: O’Reilly Media, CABook
  23. 23. book = {title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("2010-09-24"),pages: 216,language: "English",publisher: {name: "O’Reilly Media",founded: "1980",location: "CA"}}Modeling Books – EmbeddedPublisher
  24. 24. publisher = {name: "O’Reilly Media",founded: "1980",location: "CA"}book = {title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("2010-09-24"),pages: 216,language: "English"}Modeling Books & PublisherRelationship
  25. 25. publisher = {_id: "oreilly",name: "O’Reilly Media",founded: "1980",location: "CA"}book = {title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("2010-09-24"),pages: 216,language: "English",publisher_id: "oreilly"}Publisher _id as a Foreign Key
  26. 26. publisher = {name: "O’Reilly Media",founded: "1980",location: "CA"books: [ "123456789", ... ]}book = {_id: "123456789",title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("2010-09-24"),pages: 216,language: "English"}Book _id as a Foreign Key
  27. 27. Where Do You Put the Foreign Key?•  Array of books inside of publisher–  Makes sense when many means a handful of items–  Useful when items have bound on potential growth•  Reference to single publisher on books–  Useful when items have unbounded growth (unlimited # ofbooks)
  28. 28. Another ExampleOne to Many Relations
  29. 29. Books and Patrons•  Book can be checked out by one Patron at a time•  Patrons can check out many books (but not 1000’s)
  30. 30. patron = {_id: "joe",name: "Joe Bookreader",join_date: ISODate("2011-10-15"),address: { ... }}book = {_id: "123456789",title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],...}Modeling Checkouts
  31. 31. patron = {_id: "joe",name: "Joe Bookreader",join_date: ISODate("2011-10-15"),address: { ... },checked_out: [{ _id: "123456789", checked_out: "2012-10-15" },{ _id: "987654321", checked_out: "2012-09-12" },...]}Modeling Checkouts
  32. 32. DenormalizationProvides data locality
  33. 33. patron = {_id: "joe",name: "Joe Bookreader",join_date: ISODate("2011-10-15"),address: { ... },checked_out: [{ _id: "123456789",title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],checked_out: ISODate("2012-10-15")},{ _id: "987654321"title: "MongoDB: The Scaling Adventure",...}, ...]}Modeling Checkouts: Denormalized
  34. 34. Referencing vs. Embedding•  Embedding is a bit like pre-joined data•  Document-level ops are“easier”•  Embed when themanyobjects always appearwith (i.e.viewed in the context of) their parent•  Reference when you need more flexibility
  35. 35. An ExampleSingle Table Inheritance
  36. 36. book = {title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("2010-09-24"),kind: "loanable",locations: [ ... ],pages: 216,language: "English",publisher: {name: "O’Reilly Media",founded: "1980",location: "CA"}}Single Table Inheritance
  37. 37. An ExampleMany to Many Relations
  38. 38. book = {title: "MongoDB: The Definitive Guide",authors: [{ _id: "kchodorow", name: "K-Awesome" },{ _id: "mdirolf", name: "Batman Mike" },]published_date: ISODate("2010-09-24"),pages: 216,language: "English"}author = {_id: "kchodorow",name: "Kristina Chodorow",hometown: "New York"}Books and Authors
  39. 39. book = {_id: 123456789,title: "MongoDB: The Definitive Guide",authors = [ "kchodorow", "mdirolf" ],published_date: ISODate("2010-09-24"),pages: 216,language: "English"}author = {_id: "kchodorow",name: "Kristina Chodorow",hometown: "Cincinnati",books: [ 123456789, ... ]}Relation stored on both sides
  40. 40. An ExampleQueues
  41. 41. book = {_id: 123456789,title: "MongoDB: The Definitive Guide",authors: [ "Kristina Chodorow", "Mike Dirolf" ],published_date: ISODate("2010-09-24"),pages: 216,language: "English",available: 3}db.books.findAndModify({query: { _id: 123456789, available: { "$gt": 0 } },update: { $inc: { available: -1 } }})Book Document
  42. 42. mongoosejs maintainer, 10genAaron Heckmann@mongodbThank You

×