SlideShare a Scribd company logo
#MongoDBDays




Schema Design
Craig Wilson
Software Engineer, 10gen
@craiggwilson
Agenda
• Working with Documents
• Schema Design by Example
• Common Patterns




                    Single Table En
RDBMS             MongoDB
Database   ➜ Database
Table      ➜ Collection
Row        ➜ Document
Index      ➜ Index
Join       ➜ Embedding & Linking


Terminology
Example Schema (MongoDB)
Embedding




Example Schema (MongoDB)
Embedding




     Linking




Example Schema (MongoDB)
Working with Documents
Documents
Provide flexibility and
performance
Traditional Schema Design
Focuses on data storage
Document Schema Design
Focuses on data use
Document Schema Design
• Read Heavy?
• Write Heavy?
   – Document Growth

• Analytics
   – Map Reduce
   – External System
Tools for Working with Data
• Dynamic Schemas
• Embedded data structures
• Ad-hoc queries
   – Simple Queries
   – Aggregation Framework

• Secondary indexes
• Multi-Key indexes
Tools for Manipulating Data
• On the way out
  – Scalar: $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte, $ne
  – Vector: $in, $nin, $all, $size

• On the way in
  – Scalar: $inc, $set, $unset
  – Vector: $push, $pop, $pull, $pushAll, $pullAll, $addToSet
Schema Design by
Example
Library Management Application
• Patrons
• Books
• Authors
• Publishers
Use Case #1
As a Librarian, when I
swipe a patron’s card, I
need to verify their
address.
Modeling Patrons

patron = {                   patron = {
  _id: "joe“,                  _id: "joe",
  name: "Joe Bookreader”       name: "Joe Bookreader",
}                              address: {
                                  street: "123 Fake St. ",
address = {                       city: "Faketon",
  _id = "joe“,
                                  state: "MA",
  street: "123 Fake St. ",
  city: "Faketon",                zip: 12345
  state: "MA",                 }
  zip: 12345                 }
}
One to One Relations
• “Belongs to” relationships are often embedded
• Document model provides a holistic
 representation of objects with embedded entities
• Optimized for read performance
Use Case #2
As a Librarian, I want to
store multiple addresses
so I have a better chance
of getting my book back.
Modeling Patrons


patron = {                      patron = {
  _id: "joe",                     _id: "joe",
  name: "Joe Bookreader",         name: "Joe Bookreader",
  address: {                      join_date: ISODate("2011-10-15"),
                                  addresses: [
     street: "123 Fake St. ",
                                    {street: "1 Vernon St.", city: "Newton", …},
     city: "Faketon",               {street: "52 Main St.", city: "Boston", …},
     state: "MA",                 ]
     zip: 12345                 }
  }
}
Use Case #3
As a Librarian, I want to
see to the publisher of a
book.
Publishers and Books
• Publishers put out many books
• Books have one publisher
Book Data


MongoDB: The Definitive Guide,
By Kristina Chodorow and Mike Dirolf
Published: 9/24/2010
Pages: 216
Language: English

Publisher: O’Reilly Media, CA
Book Model with Embedded
Publisher

book = {
  _id: “123”,
  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"
  }
}
Book Model with Embedded
Publisher
• Optimized for read performance of Books
• Other queries are difficult
   – All publishers
Use Case #4
As a Librarian, I want to
see all the publishers in
the system.
Book Model with a Publisher
Link
publisher = {
  _id: “oreilly”,
  name: "O’Reilly Media",
  founded: "1980",
  location: "CA"
}

book = {
  _id: “123”,
  publisher_id: “oreilly”,
  title: "MongoDB: The Definitive Guide",
  authors: [ "Kristina Chodorow", "Mike Dirolf" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English"
}
Use Case #5
As a Librarian, I want to
see all the books a
publisher has published.
Publisher Model with Book
Links
publisher = {
  _id: “oreilly”,
  name: "O’Reilly Media",
  founded: "1980",
  location: "CA“,
  books: [“123”,…]
}

book = {
  _id: “123”,
  title: "MongoDB: The Definitive Guide",
  authors: [ "Kristina Chodorow", "Mike Dirolf" ]
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English"
}
Use Case #6
As a Librarian, I want to
find the author(s) of
book “Foo”.
Books and Authors
book = {
  _id: “123”,
  title: "MongoDB: The Definitive Guide",
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English"
}

author = {
  _id: “kchodorow”,
  name: "Kristina Chodorow",
  hometown: "New York"
}

author = {
  _id: “mdirolf”,
  name: “Mike Dirolf",
  hometown: “Albany"
}
Relation stored on book end
book = {
  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: "New York"
}
author = {
  _id: “mdirolf”,
  name: “Mike Dirolf",
  hometown: “Albany"
}
Relation stored on book end
book = {
  title: "MongoDB: The Definitive Guide",
  authors = [
      { id: "kchodorow", name: "Kristina Chodorow” },
      { id: "mdirolf", name: "Mike Dirolf” }
  ]
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English"
}
author = {
  _id: "kchodorow",
  name: "Kristina Chodorow",
  hometown: "New York"
}
author = {
  _id: “mdirolf”,
  name: “Mike Dirolf",
  hometown: “Albany"
}
Use Case #7
As a Librarian, I want to
find other books written
by the same author.
Relation stored on author end

book = {
  _id: “123”,
  title: "MongoDB: The Definitive Guide",
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English"
}

author = {
  _id: "kchodorow",
  name: "Kristina Chodorow",
  hometown: "Cincinnati",
  books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ]
}
Relation stored on both sides

book = {
  _id: “123”,
  title: "MongoDB: The Definitive Guide",
  authors = [
      { id: "kchodorow", name: "Kristina Chodorow” },
      { id: "mdirolf", name: "Mike Dirolf” }
  ]
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English"
}

author = {
  _id: "kchodorow",
  name: "Kristina Chodorow",
  hometown: "Cincinnati",
  books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ]

}
Linking vs. Embedding
• Embedding
   – Great for read performance
      • One seek to load entire object
      • One roundtrip to database
   – Writes can be slow
   – Maintaining data integrity

• Linking
   – More flexibility
   – Data integrity is maintained
   – Work is done during reads
Common Patterns
An Example
Trees
Parent Links

book = {
  title: "MongoDB: The Definitive Guide",
  authors: [ "Kristina Chodorow", "Mike Dirolf" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  category: "MongoDB"
}

category = { _id: ”MongoDB”, parent: “Databases” }
category = { _id: ”Databases”, parent: “Programming” }
Array of Ancestors
book = {
  title: "MongoDB: The Definitive Guide",
  authors: [ "Kristina Chodorow", "Mike Dirolf" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  parent: "MongoDB",
  categories: ["MongoDB", "Databases", "Programming" ]
}

book = {
  title: "MySQL: The Definitive Guide",
  authors: [ ”Michael Kofler" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  parent: "MySQL",
  categories : ["MySQL", "Databases", "Programming" ]
}
Ancestors as path
book = {
  title: "MongoDB: The Definitive Guide",
  authors: [ "Kristina Chodorow", "Mike Dirolf" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  category: "MongoDB/Databases/Programming"
}

book = {
  title: "MySQL: The Definitive Guide",
  authors: [ ”Michael Kofler" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  parent: "MySQL",
  category: "MySQL/Databases/Programming"
}
An Example
Inheritance
Single Table Inheritance

   id        type      area      radius    length   width
   1         circle    3.14      1
   2         square    4                   2
   3         rect      10                  5        2

   • Sparse data
   • Is missing value not required or an error?
Single collection (table)
inheritance - MongoDB
> db.shapes.find()

{ _id : 1, type: "circle", area : 3.14, radius : 1 }
{ _id : 2, type: "square", area : 4, length : 2 }
{ _id : 3, type: "rect", area : 10, length : 5, width : 2 }
Summary
• Schema design is different in MongoDB
• Basic data design principals stay the same
• Focus on how application accesses/manipulates
 data
• Rapidly evolve schema to meet your
 requirements
#MongoDBDays




Thank You
Craig Wilson
Software Engineer, 10gen
@craiggwilson

More Related Content

PPTX
Schema design mongo_boston
MongoDB
 
PPTX
Schema Design
MongoDB
 
PPTX
Webinar: Schema Design
MongoDB
 
PDF
Schema Design
MongoDB
 
PDF
Schema & Design
MongoDB
 
PPTX
Schema Design
MongoDB
 
PDF
Schema design
MongoDB
 
PDF
Schema Design
MongoDB
 
Schema design mongo_boston
MongoDB
 
Schema Design
MongoDB
 
Webinar: Schema Design
MongoDB
 
Schema Design
MongoDB
 
Schema & Design
MongoDB
 
Schema Design
MongoDB
 
Schema design
MongoDB
 
Schema Design
MongoDB
 

What's hot (19)

PDF
Schema Design
MongoDB
 
PPT
MongoDB Schema Design
MongoDB
 
PDF
Schema Design
MongoDB
 
PPTX
Back to Basics 1: Thinking in documents
MongoDB
 
PDF
Mongo DB schema design patterns
joergreichert
 
PPTX
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB
 
KEY
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
PDF
Schema Design
MongoDB
 
PPTX
Building Your First App with MongoDB
MongoDB
 
PPTX
Dev Jumpstart: Schema Design Best Practices
MongoDB
 
PPTX
Schema Design
MongoDB
 
PDF
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
PPT
Building web applications with mongo db presentation
Murat Çakal
 
PDF
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
 
PPTX
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
PPTX
Jumpstart: Schema Design
MongoDB
 
PDF
MongoDB Schema Design
aaronheckmann
 
PPTX
Webinar: Schema Design
MongoDB
 
PPTX
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Schema Design
MongoDB
 
MongoDB Schema Design
MongoDB
 
Schema Design
MongoDB
 
Back to Basics 1: Thinking in documents
MongoDB
 
Mongo DB schema design patterns
joergreichert
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB
 
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
Schema Design
MongoDB
 
Building Your First App with MongoDB
MongoDB
 
Dev Jumpstart: Schema Design Best Practices
MongoDB
 
Schema Design
MongoDB
 
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
Building web applications with mongo db presentation
Murat Çakal
 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
 
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
Jumpstart: Schema Design
MongoDB
 
MongoDB Schema Design
aaronheckmann
 
Webinar: Schema Design
MongoDB
 
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Ad

Similar to Schema Design (20)

PDF
Schema Design
MongoDB
 
PPTX
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
PDF
Schema Design
MongoDB
 
PPTX
Modeling JSON data for NoSQL document databases
Ryan CrawCour
 
PDF
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
KEY
Modeling Data in MongoDB
lehresman
 
PPTX
Schema Design
MongoDB
 
PDF
MongoDB and Schema Design
Matias Cascallares
 
PPTX
Schema Design
MongoDB
 
PPTX
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
PPTX
Jumpstart: Introduction to Schema Design
MongoDB
 
KEY
Schema design
christkv
 
PDF
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
PDF
Data_Modeling_MongoDB.pdf
jill734733
 
KEY
Schema Design (Mongo Austin)
MongoDB
 
PPTX
Schema Design Best Practices with Buzz Moschetti
MongoDB
 
KEY
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
PDF
Schema Design in MongoDB - TriMug Meetup North Carolina
Randall Hunt
 
PPTX
Document databases
Qframe
 
PPTX
tranSMART Community Meeting 5-7 Nov 13 - Session 2: MongoDB: What, Why And When
David Peyruc
 
Schema Design
MongoDB
 
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
Schema Design
MongoDB
 
Modeling JSON data for NoSQL document databases
Ryan CrawCour
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
Modeling Data in MongoDB
lehresman
 
Schema Design
MongoDB
 
MongoDB and Schema Design
Matias Cascallares
 
Schema Design
MongoDB
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
Jumpstart: Introduction to Schema Design
MongoDB
 
Schema design
christkv
 
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
Data_Modeling_MongoDB.pdf
jill734733
 
Schema Design (Mongo Austin)
MongoDB
 
Schema Design Best Practices with Buzz Moschetti
MongoDB
 
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
Schema Design in MongoDB - TriMug Meetup North Carolina
Randall Hunt
 
Document databases
Qframe
 
tranSMART Community Meeting 5-7 Nov 13 - Session 2: MongoDB: What, Why And When
David Peyruc
 
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB
 
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB
 
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB
 
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB
 
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB
 
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB
 
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB
 
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB
 
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB
 
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB
 
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB
 
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB
 

Schema Design

  • 1. #MongoDBDays Schema Design Craig Wilson Software Engineer, 10gen @craiggwilson
  • 2. Agenda • Working with Documents • Schema Design by Example • Common Patterns Single Table En
  • 3. RDBMS MongoDB Database ➜ Database Table ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedding & Linking Terminology
  • 6. Embedding Linking Example Schema (MongoDB)
  • 11. Document Schema Design • Read Heavy? • Write Heavy? – Document Growth • Analytics – Map Reduce – External System
  • 12. Tools for Working with Data • Dynamic Schemas • Embedded data structures • Ad-hoc queries – Simple Queries – Aggregation Framework • Secondary indexes • Multi-Key indexes
  • 13. Tools for Manipulating Data • On the way out – Scalar: $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte, $ne – Vector: $in, $nin, $all, $size • On the way in – Scalar: $inc, $set, $unset – Vector: $push, $pop, $pull, $pushAll, $pullAll, $addToSet
  • 15. Library Management Application • Patrons • Books • Authors • Publishers
  • 16. Use Case #1 As a Librarian, when I swipe a patron’s card, I need to verify their address.
  • 17. Modeling Patrons patron = { patron = { _id: "joe“, _id: "joe", name: "Joe Bookreader” name: "Joe Bookreader", } address: { street: "123 Fake St. ", address = { city: "Faketon", _id = "joe“, state: "MA", street: "123 Fake St. ", city: "Faketon", zip: 12345 state: "MA", } zip: 12345 } }
  • 18. One to One Relations • “Belongs to” relationships are often embedded • Document model provides a holistic representation of objects with embedded entities • Optimized for read performance
  • 19. Use Case #2 As a Librarian, I want to store multiple addresses so I have a better chance of getting my book back.
  • 20. Modeling Patrons patron = { patron = { _id: "joe", _id: "joe", name: "Joe Bookreader", name: "Joe Bookreader", address: { join_date: ISODate("2011-10-15"), addresses: [ street: "123 Fake St. ", {street: "1 Vernon St.", city: "Newton", …}, city: "Faketon", {street: "52 Main St.", city: "Boston", …}, state: "MA", ] zip: 12345 } } }
  • 21. Use Case #3 As a Librarian, I want to see to the publisher of a book.
  • 22. Publishers and Books • Publishers put out many books • Books have one publisher
  • 23. Book Data MongoDB: The Definitive Guide, By Kristina Chodorow and Mike Dirolf Published: 9/24/2010 Pages: 216 Language: English Publisher: O’Reilly Media, CA
  • 24. Book Model with Embedded Publisher book = { _id: “123”, 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" } }
  • 25. Book Model with Embedded Publisher • Optimized for read performance of Books • Other queries are difficult – All publishers
  • 26. Use Case #4 As a Librarian, I want to see all the publishers in the system.
  • 27. Book Model with a Publisher Link publisher = { _id: “oreilly”, name: "O’Reilly Media", founded: "1980", location: "CA" } book = { _id: “123”, publisher_id: “oreilly”, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English" }
  • 28. Use Case #5 As a Librarian, I want to see all the books a publisher has published.
  • 29. Publisher Model with Book Links publisher = { _id: “oreilly”, name: "O’Reilly Media", founded: "1980", location: "CA“, books: [“123”,…] } book = { _id: “123”, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" }
  • 30. Use Case #6 As a Librarian, I want to find the author(s) of book “Foo”.
  • 31. Books and Authors book = { _id: “123”, title: "MongoDB: The Definitive Guide", published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: “kchodorow”, name: "Kristina Chodorow", hometown: "New York" } author = { _id: “mdirolf”, name: “Mike Dirolf", hometown: “Albany" }
  • 32. Relation stored on book end book = { 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: "New York" } author = { _id: “mdirolf”, name: “Mike Dirolf", hometown: “Albany" }
  • 33. Relation stored on book end book = { title: "MongoDB: The Definitive Guide", authors = [ { id: "kchodorow", name: "Kristina Chodorow” }, { id: "mdirolf", name: "Mike Dirolf” } ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York" } author = { _id: “mdirolf”, name: “Mike Dirolf", hometown: “Albany" }
  • 34. Use Case #7 As a Librarian, I want to find other books written by the same author.
  • 35. Relation stored on author end book = { _id: “123”, title: "MongoDB: The Definitive Guide", published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "Cincinnati", books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ] }
  • 36. Relation stored on both sides book = { _id: “123”, title: "MongoDB: The Definitive Guide", authors = [ { id: "kchodorow", name: "Kristina Chodorow” }, { id: "mdirolf", name: "Mike Dirolf” } ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "Cincinnati", books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ] }
  • 37. Linking vs. Embedding • Embedding – Great for read performance • One seek to load entire object • One roundtrip to database – Writes can be slow – Maintaining data integrity • Linking – More flexibility – Data integrity is maintained – Work is done during reads
  • 40. Parent Links book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", category: "MongoDB" } category = { _id: ”MongoDB”, parent: “Databases” } category = { _id: ”Databases”, parent: “Programming” }
  • 41. Array of Ancestors book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", parent: "MongoDB", categories: ["MongoDB", "Databases", "Programming" ] } book = { title: "MySQL: The Definitive Guide", authors: [ ”Michael Kofler" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", parent: "MySQL", categories : ["MySQL", "Databases", "Programming" ] }
  • 42. Ancestors as path book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", category: "MongoDB/Databases/Programming" } book = { title: "MySQL: The Definitive Guide", authors: [ ”Michael Kofler" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", parent: "MySQL", category: "MySQL/Databases/Programming" }
  • 44. Single Table Inheritance id type area radius length width 1 circle 3.14 1 2 square 4 2 3 rect 10 5 2 • Sparse data • Is missing value not required or an error?
  • 45. Single collection (table) inheritance - MongoDB > db.shapes.find() { _id : 1, type: "circle", area : 3.14, radius : 1 } { _id : 2, type: "square", area : 4, length : 2 } { _id : 3, type: "rect", area : 10, length : 5, width : 2 }
  • 46. Summary • Schema design is different in MongoDB • Basic data design principals stay the same • Focus on how application accesses/manipulates data • Rapidly evolve schema to meet your requirements
  • 47. #MongoDBDays Thank You Craig Wilson Software Engineer, 10gen @craiggwilson

Editor's Notes

  • #5: Concrete example of typical blog using a document oriented de-normalized approach
  • #9: Represent rich data structures and complex relationships while keeping that data together on disk.
  • #10: Focus on the way we store our data, neglecting the way we use it.
  • #11: Document design cares first about how it’s used and we let that drive how we store the data.
  • #13: Tools for data access
  • #18: Slow to get address data every time you query for a user. Requires an extra operation.
  • #21: Patron may have multiple addressesWith MongoDB, you simply start storing the address field as an array
  • #25: Data duplication is OK!Publisher is immutable.
  • #26: Best way to figure out something is going to perform is to measure.
  • #28: What happens when oreilly moves? Do all the books have their publisher location changed?
  • #30: Keep in mind that consistently growing documents is not good.
  • #32: To get the authors given a book:- Single queryTo get books by a particular author: - get the author id - get books that have that author id in array
  • #33: To get the authors given a book:- Single queryTo get books by a particular author: - get the author id - get books that have that author id in array
  • #34: To get the authors given a book:- Single queryTo get books by a particular author: - get the author id - get books that have that author id in array
  • #36: Getting the title of book published by an author is a single queryGetting the authors of a book. 2 queriesGet the book idQuery the author for books in the id
  • #38: Rule is to measure.
  • #41: Easy to query by parent category.Hard to find in subcategories.
  • #43: Immediate parent is regexp query that is anchored to beginningAnywhere in the hierarchy is a regexp query.Not indexedHierachy information cannot be changed