SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Downloaden Sie, um offline zu lesen
MongoDB Miami February 7, 2013




Schema Design
Jeremy Mikola
Software Engineer, 10gen
Agenda
•  Working with documents
•  Evolving a Schema
•  Queries and Indexes
•  Common Patterns




                         Single Table En
RDBMS                MongoDB
Database      ➜ Database
Table         ➜ Collection
Row           ➜ Document
Index         ➜ Index
Join          ➜ Embedded Document
Foreign Key   ➜ Reference

Terminology
Working with Documents
MO N
     XRays


    Checkups



    Allergies




Modeling Data
Documents
Provide flexibility and
performance
Category
                  ·Name
                  ·URL



                          Article           Tag
       User       ·Name
 ·Name            ·Slug             ·Name
 ·Email address   ·Publish date     ·URL
                  ·Text



                     Comment
                  ·Comment
                  ·Date
                  ·Author




Normalized Data
Article
                      ·Name
                      ·Slug
                      ·Publish date
                      ·Text
                      ·Author
           User
     ·Name
                          Comment[]
                       ·Comment
     ·Email address
                       ·Date
                       ·Author


                                Tag[]
                       ·Value


                          Category[]
                       ·Value




De-Normalized (embedded) Data
Relational Schema Design
Focus on data storage
Document Schema Design
Focus on data use
Schema Design Considerations
•  How do we manipulate the data?
    –  Dynamic Ad-Hoc Queries
    –  Atomic Updates
    –  Map Reduce

•  What are the access patterns of the application?
    –  Read/Write Ratio
    –  Types of Queries / Updates
    –  Data life-cycle and growth rate
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
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
Getting Started
Library Management Application
•  Patrons
•  Books
•  Authors
•  Publishers
An Example
One to One Relations
Modeling Patrons

                             patron = {
patron = {                     _id: "joe",
  _id: "joe",                  name: "Joe Bookreader",
                               address: {
  name: "Joe Bookreader”
                                  street: "123 Fake St. ",
}                                 city: "Faketon",
                                  state: "MA",
address = {                       zip: 12345
  patron_id = "joe",           }
  street: "123 Fake St. ",   }
  city: "Faketon",
  state: "MA",
  zip: 12345
}
One to One Relations
•  Mostly the same as the relational approach
•  Generally good idea to embed “contains”
 relationships
•  Document model provides a holistic
 representation of objects
An Example
One To Many Relations
Modeling Patrons
patron = {
  _id: "joe",
  name: "Joe Bookreader",
  join_date: ISODate("2011-10-15"),
  addresses: [
    {street: "1 Vernon St.", city: "Newton", state: "MA", …},
    {street: "52 Main St.", city: "Boston", state: "MA", …}
  ]
}
Publishers and Books
•  Publishers put out many books
•  Books have one publisher
Book


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

Publisher: O’Reilly Media, CA
Modeling Books – Embedded
Publisher

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 & Publisher
Relationship
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"
}
Publisher _id as a Foreign Key
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"
}
Book _id as a Foreign Key
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"
}
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 #
       of books)
•  SQL doesn’t give you a choice, no arrays
Another Example
One 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)
Modeling Checkouts
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
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" },
     ...
  ]
}
Denormalization
Provides data locality




           De-normalize for speed
Modeling Checkouts: Denormalized
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",
                          ...
     }, ...
  ]
}
Referencing vs. Embedding
•  Embedding is a bit like pre-joined data
•  Document-level ops are easy for server to handle
•  Embed when the 'many' objects always appear
 with (i.e. viewed in the context of) their parent
•  Reference when you need more flexibility
An Example
Single Table Inheritance
Single Table Inheritance

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"
  }
}
An Example
Many to Many Relations
Category
                  ·Name
                  ·URL



                          Article           Tag
       User       ·Name
 ·Name            ·Slug             ·Name
 ·Email address   ·Publish date     ·URL
                  ·Text



                     Comment
                  ·Comment
                  ·Date
                  ·Author




Relational Approach
Books and Authors
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"
}
Relation stored on both sides

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, ... ]
}
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" }
Child Links

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

category = { _id: MongoDB, children: [ 123456789, … ] }
category = { _id: Databases, children: ["MongoDB", "Postgres"}
category = { _id: Programming, children: ["DB", "Languages"] }
Modeling Trees
•  Parent Links
   - Each node is stored as a document
   - Contains the id of the parent
•  Child Links
   - Each node contains the id’s of the children
   - Can support graphs (multiple parents / child)
Array of Ancestors
book = {
  title: "MongoDB: The Definitive Guide",
  authors: [ "Kristina Chodorow", "Mike Dirolf" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  categories: ["Programming", "Databases", "MongoDB” ]
}

book = {
  title: "MySQL: The Definitive Guide",
  authors: [ "Michael Kofler" ],
  published_date: ISODate("2010-09-24"),
  pages: 216,
  language: "English",
  parent: "MongoDB",
  ancestors: [ "Programming", "Databases", "MongoDB"]
}
An Example
Queues
Book Document

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 } }
})
MongoDB Miami, February 7, 2013




Thank You
Jeremy Mikola
Software Engineer, 10gen

Weitere ähnliche Inhalte

Was ist angesagt?

Schema design
Schema designSchema design
Schema designMongoDB
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignMongoDB
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012hungarianhc
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsMongoDB
 
Schema Design
Schema Design Schema Design
Schema Design MongoDB
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Designaaronheckmann
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patternsjoergreichert
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB
 
Jumpstart: Schema Design
Jumpstart: Schema DesignJumpstart: Schema Design
Jumpstart: Schema DesignMongoDB
 
Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDBAgile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDBStennie Steneker
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDBMongoDB
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesMongoDB
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Cataloghungarianhc
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentationMurat Çakal
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignAlex Litvinok
 

Was ist angesagt? (19)

Schema design
Schema designSchema design
Schema design
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
 
Schema Design
Schema Design Schema Design
Schema Design
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
 
Jumpstart: Schema Design
Jumpstart: Schema DesignJumpstart: Schema Design
Jumpstart: Schema Design
 
Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDBAgile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDB
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentation
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 

Andere mochten auch

MongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - WelcomeMongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - WelcomeMongoDB
 
MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB Atlas
MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB AtlasMongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB Atlas
MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB AtlasMongoDB
 
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB
 
MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...
MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...
MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...MongoDB
 
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDBMongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDBMongoDB
 
MongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - WelcomeMongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - WelcomeMongoDB
 
MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...
MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...
MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...MongoDB
 

Andere mochten auch (7)

MongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - WelcomeMongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - Welcome
 
MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB Atlas
MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB AtlasMongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB Atlas
MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB Atlas
 
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB Performance
 
MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...
MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...
MongoDB Europe 2016 - Using MongoDB to Build a Fast and Scalable Content Repo...
 
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDBMongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDB
 
MongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - WelcomeMongoDB Europe 2016 - Welcome
MongoDB Europe 2016 - Welcome
 
MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...
MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...
MongoDB Europe 2016 - Choosing Between 100 Billion Travel Options – Instant S...
 

Ähnlich wie Schema Design

Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosMongoDB
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema DesignMongoDB
 
Schema design
Schema designSchema design
Schema designchristkv
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo dbMongoDB
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsMongoDB
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialSteven Francia
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Modeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databasesModeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databasesRyan CrawCour
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNeil Henegan
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data ModelingDATAVERSITY
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDBMongoDB
 
MongoDB Hadoop DC
MongoDB Hadoop DCMongoDB Hadoop DC
MongoDB Hadoop DCMike Dirolf
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsMongoDB
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009Mike Dirolf
 
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...Prasoon Kumar
 

Ähnlich wie Schema Design (20)

Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
Schema design
Schema designSchema design
Schema design
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
 
MongoDB for Genealogy
MongoDB for GenealogyMongoDB for Genealogy
MongoDB for Genealogy
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Modeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databasesModeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databases
 
MongoDB at RuPy
MongoDB at RuPyMongoDB at RuPy
MongoDB at RuPy
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDB
 
MongoDB Hadoop DC
MongoDB Hadoop DCMongoDB Hadoop DC
MongoDB Hadoop DC
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
 
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
 

Mehr von MongoDB

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

Mehr von MongoDB (20)

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

Schema Design

  • 1. MongoDB Miami February 7, 2013 Schema Design Jeremy Mikola Software Engineer, 10gen
  • 2. Agenda •  Working with documents •  Evolving a Schema •  Queries and Indexes •  Common Patterns Single Table En
  • 3. RDBMS MongoDB Database ➜ Database Table ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Terminology
  • 5. MO N XRays Checkups Allergies Modeling Data
  • 7. Category ·Name ·URL Article Tag User ·Name ·Name ·Slug ·Name ·Email address ·Publish date ·URL ·Text Comment ·Comment ·Date ·Author Normalized Data
  • 8. Article ·Name ·Slug ·Publish date ·Text ·Author User ·Name Comment[] ·Comment ·Email address ·Date ·Author Tag[] ·Value Category[] ·Value De-Normalized (embedded) Data
  • 11. Schema Design Considerations •  How do we manipulate the data? –  Dynamic Ad-Hoc Queries –  Atomic Updates –  Map Reduce •  What are the access patterns of the application? –  Read/Write Ratio –  Types of Queries / Updates –  Data life-cycle and growth rate
  • 12. 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
  • 13. 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. Library Management Application •  Patrons •  Books •  Authors •  Publishers
  • 16. An Example One to One Relations
  • 17. Modeling Patrons patron = { patron = { _id: "joe", _id: "joe", name: "Joe Bookreader", address: { name: "Joe Bookreader” street: "123 Fake St. ", } city: "Faketon", state: "MA", address = { zip: 12345 patron_id = "joe", } street: "123 Fake St. ", } city: "Faketon", state: "MA", zip: 12345 }
  • 18. One to One Relations •  Mostly the same as the relational approach •  Generally good idea to embed “contains” relationships •  Document model provides a holistic representation of objects
  • 19. An Example One To Many Relations
  • 20. Modeling Patrons patron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), addresses: [ {street: "1 Vernon St.", city: "Newton", state: "MA", …}, {street: "52 Main St.", city: "Boston", state: "MA", …} ] }
  • 21. Publishers and Books •  Publishers put out many books •  Books have one publisher
  • 22. Book MongoDB: The Definitive Guide, By Kristina Chodorow and Mike Dirolf Published: 9/24/2010 Pages: 216 Language: English Publisher: O’Reilly Media, CA
  • 23. Modeling Books – Embedded Publisher 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" } }
  • 24. Modeling Books & Publisher Relationship 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" }
  • 25. Publisher _id as a Foreign Key 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" }
  • 26. Book _id as a Foreign Key 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" }
  • 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 # of books) •  SQL doesn’t give you a choice, no arrays
  • 28. Another Example One to Many Relations
  • 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. Modeling Checkouts 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" ], ... }
  • 31. Modeling Checkouts 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" }, ... ] }
  • 32. Denormalization Provides data locality De-normalize for speed
  • 33. Modeling Checkouts: Denormalized 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", ... }, ... ] }
  • 34. Referencing vs. Embedding •  Embedding is a bit like pre-joined data •  Document-level ops are easy for server to handle •  Embed when the 'many' objects always appear with (i.e. viewed in the context of) their parent •  Reference when you need more flexibility
  • 35. An Example Single Table Inheritance
  • 36. Single Table Inheritance 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" } }
  • 37. An Example Many to Many Relations
  • 38. Category ·Name ·URL Article Tag User ·Name ·Name ·Slug ·Name ·Email address ·Publish date ·URL ·Text Comment ·Comment ·Date ·Author Relational Approach
  • 39. Books and Authors 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" }
  • 40. Relation stored on both sides 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, ... ] }
  • 42. 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" }
  • 43. Child Links book = { _id: 123456789, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English" } category = { _id: MongoDB, children: [ 123456789, … ] } category = { _id: Databases, children: ["MongoDB", "Postgres"} category = { _id: Programming, children: ["DB", "Languages"] }
  • 44. Modeling Trees •  Parent Links - Each node is stored as a document - Contains the id of the parent •  Child Links - Each node contains the id’s of the children - Can support graphs (multiple parents / child)
  • 45. Array of Ancestors book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", categories: ["Programming", "Databases", "MongoDB” ] } book = { title: "MySQL: The Definitive Guide", authors: [ "Michael Kofler" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", parent: "MongoDB", ancestors: [ "Programming", "Databases", "MongoDB"] }
  • 47. Book Document 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 } } })
  • 48. MongoDB Miami, February 7, 2013 Thank You Jeremy Mikola Software Engineer, 10gen