SlideShare uma empresa Scribd logo
1 de 48
Baixar para ler offline
Schema Design
Software Engineer, MongoDB
Jeremy Mikola
#MongoDB
Agenda
•  Working with documents
•  Evolving a Schema
•  Queries and Indexes
•  Common Patterns
TerminologyRDBMS MongoDB
Database ➜ Database
Table ➜ Collection
Row ➜ Document
Index ➜ Index
Join ➜ Embedded Document
Foreign Key ➜ Reference
Working with Documents
Modeling Data
M O N
XRays
Checkups
Allergies
Documents
Provide flexibility and
performance
User
·Name
·Email address
Category
·Name
·URL
Comment
·Comment
·Date
·Author
Article
·Name
·Slug
·Publish date
·Text
Tag
·Name
·URL
Normalized Data
User
·Name
·Email address
Article
·Name
·Slug
·Publish date
·Text
·Author
Comment[]
·Comment
·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
patron = {
_id: "joe",
name: "Joe Bookreader”
}

address = {
patron_id = "joe",
street: "123 Fake St. ",
city: "Faketon",
state: "MA",
zip: 12345
}
Modeling Patrons




patron = {
_id: "joe",
name: "Joe Bookreader",
address: {
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
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", …}
]
}

Modeling Patrons
Publishers and Books
•  Publishers put out many books
•  Books have one publisher
MongoDB: The Definitive Guide,
By Kristina Chodorow and Mike Dirolf
Published: 9/24/2010
Pages: 216
Language: English

Publisher: O’Reilly Media, CA
Book
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 – Embedded Publisher
publisher = {
name: "O’Reilly Media",
founded: "1980",
location: "CA"
}

book = {
title: "MongoDB: The Definitive Guide",
authors: [ "Kristina Chodorow", "Mike
Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English"
}
Modeling Books & Publisher
Relationship
publisher = {
_id: "oreilly",
name: "O’Reilly Media",
founded: "1980",
location: "CA"
}

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

Publisher _id as a Foreign Key
publisher = {
name: "O’Reilly Media",
founded: "1980",
location: "CA"
books: [ "123456789", ... ]
}

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

Book _id as a Foreign Key
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)
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" },
...
]
}

Modeling Checkouts
Denormalization
Provides data locality
patron = {
_id: "joe",
name: "Joe Bookreader",
join_date: ISODate("2011-10-15"),
address: { ... },
checked_out: [
{ _id: "123456789",
title: "MongoDB: The Definitive Guide",
authors: [ "Kristina Chodorow", "Mike
Dirolf" ],
checked_out: ISODate("2012-10-15")
},
{ _id: "987654321"
title: "MongoDB: The Scaling Adventure", 

 
 
 
 
 
 ...
}, ...
]
}

Modeling Checkouts: Denormalized
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
book = {
title: "MongoDB: The Definitive Guide",
authors: [ "Kristina Chodorow", "Mike
Dirolf" ],
published_date: ISODate("2010-09-24"),
kind: "loanable",
locations: [ ... ],
pages: 216,
language: "English",
publisher: {
name: "O’Reilly Media",
founded: "1980",
location: "CA"
}
}
Single Table Inheritance
An Example
Many to Many Relations
Category
Name
URL
Article
Name
Slug
Publish date
Text
User
Name
Email address
Tag
Name
URL
Comment
Comment
Date
Author
Relational Approach
book = {
title: "MongoDB: The Definitive Guide",
authors = [
{ _id: "kchodorow", name: "K-Awesome" }, 
{ _id: "mdirolf", name: "Batman Mike" },
]
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English"
}

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

Books and Authors
book = {
_id: 123456789,
title: "MongoDB: The Definitive Guide",
authors = [ "kchodorow", "mdirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English"
}

author = { 
_id: "kchodorow",
name: "Kristina Chodorow",
hometown: "Cincinnati",
books: [ 123456789, ... ]
}
Relation stored on both sides
An Example
Trees
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" }




Parent 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"] }





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

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

db.books.findAndModify({
query: { _id: 123456789, available: { "$gt":
0 } },
update: { $inc: { available: -1 } }
})

Book Document
Thank You
Software Engineer, MongoDB
Jeremy Mikola
#MongoDB

Mais conteúdo relacionado

Mais procurados

Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Schema design
Schema designSchema design
Schema design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
MongoDB
 
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
MongoDB
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
Alex Litvinok
 
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
Murat Çakal
 

Mais procurados (19)

Schema Design
Schema DesignSchema Design
Schema Design
 
Jumpstart: Schema Design
Jumpstart: Schema DesignJumpstart: Schema Design
Jumpstart: Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema design
Schema designSchema design
Schema design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
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...
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 
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
 
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 (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 Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB 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
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
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
 
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
 
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
 

Destaque

Mongo db bangalore
Mongo db bangaloreMongo db bangalore
Mongo db bangalore
MongoDB
 
What's New in MongoDB 2.6
What's New in MongoDB 2.6What's New in MongoDB 2.6
What's New in MongoDB 2.6
MongoDB
 
MongoDB and AWS Best Practices
MongoDB and AWS Best PracticesMongoDB and AWS Best Practices
MongoDB and AWS Best Practices
MongoDB
 
Introduction to the New Aggregation Framework
Introduction to the New Aggregation FrameworkIntroduction to the New Aggregation Framework
Introduction to the New Aggregation Framework
MongoDB
 
S01 e01 schema-design
S01 e01 schema-designS01 e01 schema-design
S01 e01 schema-design
MongoDB
 
Back tobasicswebinar part6-rev.
Back tobasicswebinar part6-rev.Back tobasicswebinar part6-rev.
Back tobasicswebinar part6-rev.
MongoDB
 
Introduction to MongoDB and Ruby
Introduction to MongoDB and RubyIntroduction to MongoDB and Ruby
Introduction to MongoDB and Ruby
MongoDB
 

Destaque (13)

Webinar: Revolutionizing Application Development with MongoDB
Webinar: Revolutionizing Application Development with MongoDBWebinar: Revolutionizing Application Development with MongoDB
Webinar: Revolutionizing Application Development with MongoDB
 
Mongo db bangalore
Mongo db bangaloreMongo db bangalore
Mongo db bangalore
 
Challenges in an E-Commerce Catalogue with SQL; How Mongo helps
Challenges in an E-Commerce Catalogue with SQL; How Mongo helpsChallenges in an E-Commerce Catalogue with SQL; How Mongo helps
Challenges in an E-Commerce Catalogue with SQL; How Mongo helps
 
What's New in MongoDB 2.6
What's New in MongoDB 2.6What's New in MongoDB 2.6
What's New in MongoDB 2.6
 
MongoDB and AWS Best Practices
MongoDB and AWS Best PracticesMongoDB and AWS Best Practices
MongoDB and AWS Best Practices
 
Introduction to the New Aggregation Framework
Introduction to the New Aggregation FrameworkIntroduction to the New Aggregation Framework
Introduction to the New Aggregation Framework
 
S01 e01 schema-design
S01 e01 schema-designS01 e01 schema-design
S01 e01 schema-design
 
Partner Webinar: Deliver Big Data Apps Faster With Informatica & MongoDB
Partner Webinar: Deliver Big Data Apps Faster With Informatica & MongoDBPartner Webinar: Deliver Big Data Apps Faster With Informatica & MongoDB
Partner Webinar: Deliver Big Data Apps Faster With Informatica & MongoDB
 
MongoDB, Cloudformation and Chef
MongoDB, Cloudformation and ChefMongoDB, Cloudformation and Chef
MongoDB, Cloudformation and Chef
 
Back tobasicswebinar part6-rev.
Back tobasicswebinar part6-rev.Back tobasicswebinar part6-rev.
Back tobasicswebinar part6-rev.
 
Introduction to MongoDB and Ruby
Introduction to MongoDB and RubyIntroduction to MongoDB and Ruby
Introduction to MongoDB and Ruby
 
MongoDB and AWS: Integrations
MongoDB and AWS: IntegrationsMongoDB and AWS: Integrations
MongoDB and AWS: Integrations
 
Webinar: An Enterprise Architect’s View of MongoDB
Webinar: An Enterprise Architect’s View of MongoDBWebinar: An Enterprise Architect’s View of MongoDB
Webinar: An Enterprise Architect’s View of MongoDB
 

Semelhante a Schema Design

Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
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
DATAVERSITY
 

Semelhante a Schema Design (20)

Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
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
 
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: 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
 
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
 
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
 
Inferring Versioned Schemas from NoSQL Databases and its Applications
Inferring Versioned Schemas from NoSQL Databases and its ApplicationsInferring Versioned Schemas from NoSQL Databases and its Applications
Inferring Versioned Schemas from NoSQL Databases and its Applications
 
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
 
MongoDB
MongoDBMongoDB
MongoDB
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in Documents
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days Munich
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDB
 
MongoDB at RuPy
MongoDB at RuPyMongoDB at RuPy
MongoDB at RuPy
 
Jumpstart: Introduction to Schema Design
Jumpstart: Introduction to Schema DesignJumpstart: Introduction to Schema Design
Jumpstart: Introduction to Schema Design
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
MongoDB Hadoop DC
MongoDB Hadoop DCMongoDB Hadoop DC
MongoDB Hadoop DC
 

Mais de MongoDB

Mais de 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...
 

Último

Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al MizharAl Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
allensay1
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
daisycvs
 

Último (20)

Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business Potential
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
 
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
 
Cannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 UpdatedCannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 Updated
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptx
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Power point presentation on enterprise performance management
Power point presentation on enterprise performance managementPower point presentation on enterprise performance management
Power point presentation on enterprise performance management
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
 
Call 7737669865 Vadodara Call Girls Service at your Door Step Available All Time
Call 7737669865 Vadodara Call Girls Service at your Door Step Available All TimeCall 7737669865 Vadodara Call Girls Service at your Door Step Available All Time
Call 7737669865 Vadodara Call Girls Service at your Door Step Available All Time
 
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
New 2024 Cannabis Edibles Investor Pitch Deck Template
New 2024 Cannabis Edibles Investor Pitch Deck TemplateNew 2024 Cannabis Edibles Investor Pitch Deck Template
New 2024 Cannabis Edibles Investor Pitch Deck Template
 
Cracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareCracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' Slideshare
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al MizharAl Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
 
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAIGetting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
 

Schema Design

  • 1. Schema Design Software Engineer, MongoDB Jeremy Mikola #MongoDB
  • 2. Agenda •  Working with documents •  Evolving a Schema •  Queries and Indexes •  Common Patterns
  • 3. TerminologyRDBMS MongoDB Database ➜ Database Table ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference
  • 5. Modeling Data M O N XRays Checkups Allergies
  • 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
  • 16. An Example One to One Relations
  • 17. patron = { _id: "joe", name: "Joe Bookreader” } address = { patron_id = "joe", street: "123 Fake St. ", city: "Faketon", state: "MA", zip: 12345 } Modeling Patrons patron = { _id: "joe", name: "Joe Bookreader", address: { 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. 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", …} ] } Modeling Patrons
  • 21. Publishers and Books •  Publishers put out many books •  Books have one publisher
  • 22. MongoDB: The Definitive Guide, By Kristina Chodorow and Mike Dirolf Published: 9/24/2010 Pages: 216 Language: English Publisher: O’Reilly Media, CA Book
  • 23. book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher: { name: "O’Reilly Media", founded: "1980", location: "CA" } } Modeling Books – Embedded Publisher
  • 24. publisher = { name: "O’Reilly Media", founded: "1980", location: "CA" } book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English" } Modeling Books & Publisher Relationship
  • 25. publisher = { _id: "oreilly", name: "O’Reilly Media", founded: "1980", location: "CA" } book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher_id: "oreilly" } Publisher _id as a Foreign Key
  • 26. publisher = { name: "O’Reilly Media", founded: "1980", location: "CA" books: [ "123456789", ... ] } book = { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English" } Book _id as a Foreign Key
  • 27. 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. patron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... } } book = { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], ... } Modeling Checkouts
  • 31. patron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }, checked_out: [ { _id: "123456789", checked_out: "2012-10-15" }, { _id: "987654321", checked_out: "2012-09-12" }, ... ] } Modeling Checkouts
  • 33. patron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }, checked_out: [ { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], checked_out: ISODate("2012-10-15") }, { _id: "987654321" title: "MongoDB: The Scaling Adventure", ... }, ... ] } Modeling Checkouts: Denormalized
  • 34. 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. book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), kind: "loanable", locations: [ ... ], pages: 216, language: "English", publisher: { name: "O’Reilly Media", founded: "1980", location: "CA" } } Single Table Inheritance
  • 37. An Example Many to Many Relations
  • 39. book = { title: "MongoDB: The Definitive Guide", authors = [ { _id: "kchodorow", name: "K-Awesome" }, { _id: "mdirolf", name: "Batman Mike" }, ] published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York" } Books and Authors
  • 40. book = { _id: 123456789, title: "MongoDB: The Definitive Guide", authors = [ "kchodorow", "mdirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English" } author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "Cincinnati", books: [ 123456789, ... ] } Relation stored on both sides
  • 42. 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" } Parent Links
  • 43. 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"] } Child Links
  • 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. 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"] } Array of Ancestors
  • 47. book = { _id: 123456789, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", available: 3 } db.books.findAndModify({ query: { _id: 123456789, available: { "$gt": 0 } }, update: { $inc: { available: -1 } } }) Book Document
  • 48. Thank You Software Engineer, MongoDB Jeremy Mikola #MongoDB