SlideShare a Scribd company logo
1 of 42
Download to read offline
#MongoDBDays

Schema Design
Craig Wilson
Software Engineer, MongoDB
@craiggwilson
All application development is

Schema Design
Success comes from a

Proper Data Structure
Terminology
RDBMS

MongoDB

Database

➜ Database

Table

➜ Collection

Row

➜ Document

Index

➜ Index

Join

➜ Embedding & Linking
Working with Documents
What is a Document?
	
  
{	
  
	
  	
  	
  	
  _id:	
  “123”,	
  
	
  	
  	
  	
  title:	
  "MongoDB:	
  The	
  Definitive	
  Guide",	
  
	
  	
  	
  	
  authors:	
  [	
  
	
  	
  	
  	
  	
  	
  	
  {	
  _id:	
  "kchodorow",	
  name:	
  "Kristina	
  Chodorow“	
  },	
  
	
  	
  	
  	
  	
  	
  	
  {	
  _id:	
  "mdirold",	
  name:	
  “Mike	
  Dirolf“	
  }	
  
	
  	
  	
  	
  ],	
  
	
  	
  	
  	
  published_date:	
  ISODate("2010-­‐09-­‐24"),	
  
	
  	
  	
  	
  pages:	
  216,	
  
	
  	
  	
  	
  language:	
  "English",	
  
	
  	
  	
  	
  publisher:	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  name:	
  "O’Reilly	
  Media",	
  
	
  	
  	
  	
  	
  	
  	
  	
  founded:	
  "1980",	
  
	
  	
  	
  	
  	
  	
  	
  	
  location:	
  "CA"	
  
	
  	
  	
  	
  }	
  
}	
  
Traditional Schema Design

Focus on Data Storage
Document Schema Design

Focus on Data Usage
Traditional Schema Design

What answers do I have?
Document Schema Design

What questions do I have?
Schema Design By Example
Library Management Application
•  Patrons/Users
•  Books
•  Authors
•  Publishers
Question:

What is a Patron’s Address?
A Patron and their Address
>	
  patron	
  =	
  db.patrons.find({	
  _id	
  :	
  “joe”	
  })	
  
{	
  
	
  	
  	
  	
  _id:	
  "joe“,	
  
	
  	
  	
  	
  name:	
  "Joe	
  Bookreader”	
  
}	
  
	
  
>	
  address	
  =	
  db.addresses.find({	
  _id	
  :	
  “joe”	
  })	
  
{	
  
	
  	
  	
  	
  _id:	
  "joe“,	
  
	
  	
  	
  	
  street:	
  "123	
  Fake	
  St.	
  ",	
  
	
  	
  	
  	
  city:	
  "Faketon",	
  
	
  	
  	
  	
  state:	
  "MA",	
  
	
  	
  	
  	
  zip:	
  12345	
  
}	
  
	
  
A Patron and their Address
>	
  patron	
  =	
  db.patrons.find({	
  _id	
  :	
  “joe”	
  })	
  
{	
  
	
  	
  	
  	
  _id:	
  "joe",	
  
	
  	
  	
  	
  name:	
  "Joe	
  Bookreader",	
  
	
  	
  	
  	
  address:	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  street:	
  "123	
  Fake	
  St.	
  ",	
  
	
  	
  	
  	
  	
  	
  	
  	
  city:	
  "Faketon",	
  
	
  	
  	
  	
  	
  	
  	
  	
  state:	
  "MA",	
  
	
  	
  	
  	
  	
  	
  	
  	
  zip:	
  12345	
  
	
  	
  	
  	
  }	
  
}	
  
	
  
One-to-One Relationships
•  “Belongs to” relationships are often embedded.
•  Holistic representation of entities with their

embedded attributes and relationships.
•  Optimized for read performance
Question:

What are a Patron’s
Addresses?
A Patron and their Addresses
> patron = db.patrons.find({ _id : “bob” })
{
_id: “bob",
name: “Bob Knowitall",
addresses: [
{street: "1 Vernon St.", city: "Newton", …},
{street: "52 Main St.", city: "Boston", …},
]
}
A Patron and their Addresses
> patron = db.patrons.find({ _id : “bob” })
{
_id: “bob",
name: “Bob Knowitall",
addresses: [
{street: "1 Vernon St.", city: "Newton", …},
{street: "52 Main St.", city: "Boston", …},
]
}
> patron = db.patrons.find({ _id : “joe” })
{
_id: "joe",
name: "Joe Bookreader",
address: { street: "123 Fake St. ", city: "Faketon", …}
}
Migration Possibilities
•  Migrate all documents when the schema changes.
•  Migrate On-Demand
–  As we pull up a patron’s document, we make the change.
–  Any patrons that never come into the library never get
updated.
•  Leave it alone
–  As long as the application knows about both types…
Question:

Who is the publisher of this
book?
Book
MongoDB: The Definitive Guide,
By Kristina Chodorow and Mike Dirolf
Published: 9/24/2010
Pages: 216
Language: English

Publisher: O’Reilly Media, CA
Book with embedded Publisher
> book = db.books.find({ _id : “123” })
{
_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 with embedded Publisher
•  Optimized for read performance of Books
•  Other queries become difficult
Question:

Who are all the publishers
in the system?
All Publishers
> publishers = db.publishers.find()
{
_id: “oreilly”,
name: "O’Reilly Media",
founded: "1980",
location: "CA"
}
{
_id: “penguin”,
name: “Penguin”,
founded: “1983”,
location: “CA”
}
Book with linked Publisher
> book = db.books.find({ _id: “123” })
{
_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"
}
> db.publishers.find({ _id : book.publisher_id })
{
_id: “oreilly”,
name: "O’Reilly Media",
founded: "1980",
location: "CA"
}
Question:

What are all the books a
publisher has published?
Publisher with linked Books
> publisher = db.publishers.find({ _id : “oreilly” })
{
_id: “oreilly”,
name: "O’Reilly Media",
founded: "1980",
location: "CA“,
books: [“123”,…]
}

> books = db.books.find({ _id: { $in : publisher.books } })
Question:

Who are the authors of a
given book?
Books with linked Authors
> book = db.books.find({ _id : “123” })
{
_id: “123”,
title: "MongoDB: The Definitive Guide",
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English“,
authors: [“kchodorow”, “mdirolf”]
}
> authors = db.authors.find({ _id : { $in : book.authors } })
{ _id: "kchodorow", name: "Kristina Chodorow”, hometown: … }
{ _id: “mdirolf", name: “Mike Dirolf“, hometown: … }
Books with linked Authors
> book = db.books.find({ _id : “123” })
{
_id: “123”,
title: "MongoDB: The Definitive Guide",
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English“,
authors = [
{ id: "kchodorow", name: "Kristina Chodorow” },
{ id: "mdirolf", name: "Mike Dirolf” }
]
}
Question:

What are all the books an
author has written?
Authors with linked Books
> authors = db.authors.find({ _id : “kchodorow” })
{
_id: "kchodorow",
name: "Kristina Chodorow",
hometown: "Cincinnati",
books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ]
}
Links on both Authors and Books
> authors = db.authors.find({ _id : “kchodorow” })
{
_id: "kchodorow",
name: "Kristina Chodorow",
hometown: "Cincinnati",
books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ]
}
> book = db.books.find({ _id : “123” })
{
_id: “123”,
title: "MongoDB: The Definitive Guide",
authors = [
{ id: "kchodorow", name: "Kristina Chodorow” },
{ id: "mdirolf", name: "Mike Dirolf” }
]
}
Linking vs. Embedding
•  Embedding
–  Great for read performance
–  Writes can be slow
–  Data integrity needs to be managed
•  Linking
–  Flexible
–  Data integrity is built-in
–  Work is done during reads
Question:

What are all the books
about databases?
Categories as Documents
> book = db.books.find({ _id : “123” })
{
_id: “123”,
title: "MongoDB: The Definitive Guide",
category: “MongoDB”
}
> categories = db.categories.find({ _id: “MongoDB” })
{
_id: “MongoDB”,
parent: “Databases”
}
Categories as an Array
> book = db.books.find({ _id : “123” })
{
_id: “123”,
title: "MongoDB: The Definitive Guide",
categories: [“MongoDB”, “Databases”, “Programming”]
}
> db.books.find({ categories: “Databases” })
Categories as a Path
> book = db.books.find({ _id : “123” })
{
_id: “123”,
title: "MongoDB: The Definitive Guide",
category: “Programming/Databases/MongoDB”
}
> db.books.find({ category: ^Programming/Databases/* })
Conclusion
•  Schema design is different in MongoDB
•  Basic data design principals stay the same
•  Focus on how an application accesses/manipulates

data
•  Evolve the schema to meet requirements as they

change
#MongoDBDays

Schema Design
Craig Wilson
Software Engineer, 10gen
@craiggwilson

More Related Content

What's hot

Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Schema & Design
Schema & DesignSchema & Design
Schema & Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
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
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
MongoDB
 

What's hot (19)

Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 
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
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
Schema & Design
Schema & DesignSchema & Design
Schema & Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
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
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
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
 
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...
 
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 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
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
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
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
 

Similar to Schema Design

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

Similar to Schema Design (20)

Schema Design
Schema Design Schema Design
Schema Design
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design in MongoDB - TriMug Meetup North Carolina
Schema Design in MongoDB - TriMug Meetup North CarolinaSchema Design in MongoDB - TriMug Meetup North Carolina
Schema Design in MongoDB - TriMug Meetup North Carolina
 
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
 
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, it's not just about big data
MongoDB, it's not just about big dataMongoDB, it's not just about big data
MongoDB, it's not just about big data
 
Schema design
Schema designSchema design
Schema design
 
How to survive in a BASE world
How to survive in a BASE worldHow to survive in a BASE world
How to survive in a BASE world
 
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
 
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
 
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
 
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
 
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
 
MongoDB at GUL
MongoDB at GULMongoDB at GUL
MongoDB at GUL
 
Mongo db 101 dc group
Mongo db 101 dc groupMongo db 101 dc group
Mongo db 101 dc group
 
Schema Design
Schema DesignSchema Design
Schema Design
 
MongoDB Workshop
MongoDB WorkshopMongoDB Workshop
MongoDB Workshop
 

More from MongoDB

More from 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...
 

Recently uploaded

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
 
Mckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for ViewingMckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for Viewing
Nauman Safdar
 
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan CytotecJual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
ZurliaSoop
 
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in PakistanChallenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
vineshkumarsajnani12
 

Recently uploaded (20)

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
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
Berhampur CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDINGBerhampur CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptx
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDINGBerhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
 
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
 
Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdf
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
 
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
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDINGBerhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
 
WheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond InsightsWheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond Insights
 
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
 
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTSDurg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
 
Mckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for ViewingMckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for Viewing
 
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan CytotecJual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
 
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in PakistanChallenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
 
HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024
 

Schema Design

  • 1. #MongoDBDays Schema Design Craig Wilson Software Engineer, MongoDB @craiggwilson
  • 2. All application development is Schema Design
  • 3. Success comes from a Proper Data Structure
  • 4. Terminology RDBMS MongoDB Database ➜ Database Table ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedding & Linking
  • 6. What is a Document?   {          _id:  “123”,          title:  "MongoDB:  The  Definitive  Guide",          authors:  [                {  _id:  "kchodorow",  name:  "Kristina  Chodorow“  },                {  _id:  "mdirold",  name:  “Mike  Dirolf“  }          ],          published_date:  ISODate("2010-­‐09-­‐24"),          pages:  216,          language:  "English",          publisher:  {                  name:  "O’Reilly  Media",                  founded:  "1980",                  location:  "CA"          }   }  
  • 9. Traditional Schema Design What answers do I have?
  • 10. Document Schema Design What questions do I have?
  • 11. Schema Design By Example
  • 12. Library Management Application •  Patrons/Users •  Books •  Authors •  Publishers
  • 13. Question: What is a Patron’s Address?
  • 14. A Patron and their Address >  patron  =  db.patrons.find({  _id  :  “joe”  })   {          _id:  "joe“,          name:  "Joe  Bookreader”   }     >  address  =  db.addresses.find({  _id  :  “joe”  })   {          _id:  "joe“,          street:  "123  Fake  St.  ",          city:  "Faketon",          state:  "MA",          zip:  12345   }    
  • 15. A Patron and their Address >  patron  =  db.patrons.find({  _id  :  “joe”  })   {          _id:  "joe",          name:  "Joe  Bookreader",          address:  {                  street:  "123  Fake  St.  ",                  city:  "Faketon",                  state:  "MA",                  zip:  12345          }   }    
  • 16. One-to-One Relationships •  “Belongs to” relationships are often embedded. •  Holistic representation of entities with their embedded attributes and relationships. •  Optimized for read performance
  • 17. Question: What are a Patron’s Addresses?
  • 18. A Patron and their Addresses > patron = db.patrons.find({ _id : “bob” }) { _id: “bob", name: “Bob Knowitall", addresses: [ {street: "1 Vernon St.", city: "Newton", …}, {street: "52 Main St.", city: "Boston", …}, ] }
  • 19. A Patron and their Addresses > patron = db.patrons.find({ _id : “bob” }) { _id: “bob", name: “Bob Knowitall", addresses: [ {street: "1 Vernon St.", city: "Newton", …}, {street: "52 Main St.", city: "Boston", …}, ] } > patron = db.patrons.find({ _id : “joe” }) { _id: "joe", name: "Joe Bookreader", address: { street: "123 Fake St. ", city: "Faketon", …} }
  • 20. Migration Possibilities •  Migrate all documents when the schema changes. •  Migrate On-Demand –  As we pull up a patron’s document, we make the change. –  Any patrons that never come into the library never get updated. •  Leave it alone –  As long as the application knows about both types…
  • 21. Question: Who is the publisher of this book?
  • 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. Book with embedded Publisher > book = db.books.find({ _id : “123” }) { _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" } }
  • 24. Book with embedded Publisher •  Optimized for read performance of Books •  Other queries become difficult
  • 25. Question: Who are all the publishers in the system?
  • 26. All Publishers > publishers = db.publishers.find() { _id: “oreilly”, name: "O’Reilly Media", founded: "1980", location: "CA" } { _id: “penguin”, name: “Penguin”, founded: “1983”, location: “CA” }
  • 27. Book with linked Publisher > book = db.books.find({ _id: “123” }) { _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" } > db.publishers.find({ _id : book.publisher_id }) { _id: “oreilly”, name: "O’Reilly Media", founded: "1980", location: "CA" }
  • 28. Question: What are all the books a publisher has published?
  • 29. Publisher with linked Books > publisher = db.publishers.find({ _id : “oreilly” }) { _id: “oreilly”, name: "O’Reilly Media", founded: "1980", location: "CA“, books: [“123”,…] } > books = db.books.find({ _id: { $in : publisher.books } })
  • 30. Question: Who are the authors of a given book?
  • 31. Books with linked Authors > book = db.books.find({ _id : “123” }) { _id: “123”, title: "MongoDB: The Definitive Guide", published_date: ISODate("2010-09-24"), pages: 216, language: "English“, authors: [“kchodorow”, “mdirolf”] } > authors = db.authors.find({ _id : { $in : book.authors } }) { _id: "kchodorow", name: "Kristina Chodorow”, hometown: … } { _id: “mdirolf", name: “Mike Dirolf“, hometown: … }
  • 32. Books with linked Authors > book = db.books.find({ _id : “123” }) { _id: “123”, title: "MongoDB: The Definitive Guide", published_date: ISODate("2010-09-24"), pages: 216, language: "English“, authors = [ { id: "kchodorow", name: "Kristina Chodorow” }, { id: "mdirolf", name: "Mike Dirolf” } ] }
  • 33. Question: What are all the books an author has written?
  • 34. Authors with linked Books > authors = db.authors.find({ _id : “kchodorow” }) { _id: "kchodorow", name: "Kristina Chodorow", hometown: "Cincinnati", books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ] }
  • 35. Links on both Authors and Books > authors = db.authors.find({ _id : “kchodorow” }) { _id: "kchodorow", name: "Kristina Chodorow", hometown: "Cincinnati", books: [ {id: “123”, title : "MongoDB: The Definitive Guide“ } ] } > book = db.books.find({ _id : “123” }) { _id: “123”, title: "MongoDB: The Definitive Guide", authors = [ { id: "kchodorow", name: "Kristina Chodorow” }, { id: "mdirolf", name: "Mike Dirolf” } ] }
  • 36. Linking vs. Embedding •  Embedding –  Great for read performance –  Writes can be slow –  Data integrity needs to be managed •  Linking –  Flexible –  Data integrity is built-in –  Work is done during reads
  • 37. Question: What are all the books about databases?
  • 38. Categories as Documents > book = db.books.find({ _id : “123” }) { _id: “123”, title: "MongoDB: The Definitive Guide", category: “MongoDB” } > categories = db.categories.find({ _id: “MongoDB” }) { _id: “MongoDB”, parent: “Databases” }
  • 39. Categories as an Array > book = db.books.find({ _id : “123” }) { _id: “123”, title: "MongoDB: The Definitive Guide", categories: [“MongoDB”, “Databases”, “Programming”] } > db.books.find({ categories: “Databases” })
  • 40. Categories as a Path > book = db.books.find({ _id : “123” }) { _id: “123”, title: "MongoDB: The Definitive Guide", category: “Programming/Databases/MongoDB” } > db.books.find({ category: ^Programming/Databases/* })
  • 41. Conclusion •  Schema design is different in MongoDB •  Basic data design principals stay the same •  Focus on how an application accesses/manipulates data •  Evolve the schema to meet requirements as they change
  • 42. #MongoDBDays Schema Design Craig Wilson Software Engineer, 10gen @craiggwilson