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
PPTX
Schema Design
PPTX
Webinar: Schema Design
PDF
Schema Design
PDF
Schema & Design
PPTX
Schema Design
PDF
Schema design
PDF
Schema Design
Schema design mongo_boston
Schema Design
Webinar: Schema Design
Schema Design
Schema & Design
Schema Design
Schema design
Schema Design

What's hot (19)

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

Similar to Schema Design (20)

PDF
Schema Design
PPTX
Webinar: Back to Basics: Thinking in Documents
PDF
Schema Design
PPTX
Schema Design
KEY
Schema Design
PPTX
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
KEY
Schema design
PPTX
Modeling JSON data for NoSQL document databases
PDF
10gen Presents Schema Design and Data Modeling
PDF
10gen MongoDB Video Presentation at WebGeek DevCup
PDF
MongoDB Mojo: Building a Basic Perl App
KEY
Managing Social Content with MongoDB
KEY
OSCON 2012 MongoDB Tutorial
PPTX
Back to Basics Webinar 3 - Thinking in Documents
KEY
MongoDB for Genealogy
KEY
Schema Design (Mongo Austin)
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
PPTX
Webinar: General Technical Overview of MongoDB for Dev Teams
KEY
Introduction to MongoDB
PPTX
Schema Design
Webinar: Back to Basics: Thinking in Documents
Schema Design
Schema Design
Schema Design
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Schema design
Modeling JSON data for NoSQL document databases
10gen Presents Schema Design and Data Modeling
10gen MongoDB Video Presentation at WebGeek DevCup
MongoDB Mojo: Building a Basic Perl App
Managing Social Content with MongoDB
OSCON 2012 MongoDB Tutorial
Back to Basics Webinar 3 - Thinking in Documents
MongoDB for Genealogy
Schema Design (Mongo Austin)
Back to Basics Webinar 3: Schema Design Thinking in Documents
Webinar: General Technical Overview of MongoDB for Dev Teams
Introduction to MongoDB
Ad

More from MongoDB (20)

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

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