Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads.
#MDBlocal
#MDBlocal
#MDBlocal
Jump Start:
Introduction to Schema Design
#MDBlocal
Saurabh Kashikar
Manager – Consulting & Pre-Sales,
MongoDB
#MDBlocal#MDBlocal
Why a Schema Design Jumpstart?
• Schema Design is important in all databases
• Almost all performance i...
#MDBlocal#MDBlocal
Goals for this session
At the end of this session, you should be able to:
● Explain the document model
...
#MDBlocal#MDBlocal
High-Level Schema Design
Start with the Consumer
#MDBlocal#MDBlocal
Relational Schemas
#MDBlocal#MDBlocal
MongoDB Schemas
#MDBlocal#MDBlocal
How is Data Stored in MongoDB?
#MDBlocal#MDBlocal
MongoDB uses JSON Documents...
Why JSON? JSON is:
● a widely-used standard
● fast, lean, flexible
● sup...
#MDBlocal#MDBlocal
MongoDB Documents are Rich Data Structures
{
first_name: ‘Paul’,
surname: ‘Miller’,
cell: 447557505611,...
#MDBlocal#MDBlocal
Document Schemas are Flexible
{
sku: ‘PAINTZXC123’,
product_name: ‘Metallic Paint’,
product_type: ‘pain...
#MDBlocal#MDBlocal
Progress on Goals for this session
At the end of this session, you should be able to:
● Explain the doc...
#MDBlocal#MDBlocal
Schema Design
Considerations and Patterns
#MDBlocal#MDBlocal
Factors to Consider
● How will this data be accessed?
● Number of Reads vs. Updates
● How big will each...
#MDBlocal#MDBlocal
2 Big “Rules”:
● Consider your consumers: how will this data be used?
● Data that works together lives ...
#MDBlocal#MDBlocal
Example > Guitar Collectors
ERD
Relational
DB
MongoDB
Table

Collection
Column  Field
Row  Document
...
#MDBlocal#MDBlocal
Example > Guitar Collectors
Relational
Consider your consumers:
how will this data be used?
#MDBlocal#MDBlocal
Example > Guitar Collectors
MongoDB - Embedding
#MDBlocal#MDBlocal
Example > Guitar Collectors
Typical queries from a consumer of this data might be:
● “What guitars does...
#MDBlocal#MDBlocal
Example > Guitar Collectors
Typical queries from a consumer of this data might be:
● “What guitars does...
#MDBlocal#MDBlocal
Progress on Goals for this session
At the end of this session, you should be able to:
● Explain the doc...
#MDBlocal#MDBlocal
Schema Design Patterns
in MongoDB
#MDBlocal#MDBlocal
Design Patterns
Data Relationships
● One : One
● One : Many
● Many : Many
Learn More Here
https://docs....
#MDBlocal#MDBlocal
Design Patterns:
Real-World Examples
#MDBlocal#MDBlocal
Example > Healthcare
We want to build a mobile app for patients and providers.
Within our medical syste...
#MDBlocal#MDBlocal
1:1 Relationships
← Embed – weak entity
Medical Procedures
Data that works together
lives together.
#MDBlocal#MDBlocal
One:Many Relationships
{
_id: 2,
first: “Joe”,
last: “Patient”,
addr: { …},
procedures: [
{
id: 12345,
...
#MDBlocal#MDBlocal
Many : Many Relationship
Like a One-to-Many relationship, you can embed sub-documents or reference them...
#MDBlocal#MDBlocal
Many : Many Relationship using Embedding
{
_id: 1,
name: “Oak Valley Hospital”,
city: “New York”,
beds:...
#MDBlocal#MDBlocal
Many : Many Relationship using References
{
_id: 1,
name: “Oak Valley Hospital”,
city: “New York”,
beds...
#MDBlocal#MDBlocal
Example > Movie Rental App
We want to build a movie rental system. Our clients will want to:
● Search f...
#MDBlocal#MDBlocal
The Subset pattern
• You want to display dependent
information, but only part of it.
• The rest of the ...
#MDBlocal#MDBlocal
Example > Movie Rental App
● Search for movies by Title
● See the main cast members on
the main movie p...
#MDBlocal#MDBlocal
RECAP
#MDBlocal#MDBlocal
Progress on Goals for this session
At the end of this session, you should be able to:
● Explain the doc...
#MDBlocal#MDBlocal
Data Governance: Constrain Your Schema
#MDBlocal#MDBlocal
Rich Indexes
db.collectors.createIndex({
"location":"2dsphere",
"cars.year": 1
});
#MDBlocal#MDBlocal
Expressive Queries
#MDBlocal#MDBlocal
Key Points
● Upfront schema design in MongoDB is important
● Differs from relational, but some concepts...
#MDBlocal
#MDBlocal
THANK YOU
FOR JOINING US!
Upcoming SlideShare
Loading in …5
×

[MongoDB.local Bengaluru 2018] Jumpstart: Introduction to Schema Design

253 views

Published on

Presented by: Saurabh Kashikar

Abstract: MongoDB’s basic unit of storage is a document. Documents can represent rich, schema-free data structures, meaning that we have several viable alternatives to the normalized, relational model. If you are new to MongoDB, learn the schema design basics in this introductory session. This session will help you model basic relationships in MongoDB.

What You Will Learn:

- The fundamentals of the MongoDB document model.

- How to model 1-1 and one-to-many (1-N) relationships in MongoDB.

- How to model many-to-many (N-N) relationships in MongoDB.

Published in: Technology
  • Be the first to comment

[MongoDB.local Bengaluru 2018] Jumpstart: Introduction to Schema Design

  1. 1. #MDBlocal #MDBlocal
  2. #MDBlocal Jump Start: Introduction to Schema Design
  3. #MDBlocal Saurabh Kashikar Manager – Consulting & Pre-Sales, MongoDB
  4. #MDBlocal#MDBlocal Why a Schema Design Jumpstart? • Schema Design is important in all databases • Almost all performance issues we see in MongoDB installations are because of poor schema design decisions • MongoDB schema design is different from relational DBs • MongoDB supports some interesting new patterns But wait! Isn’t MongoDB “schema-less”?
  5. #MDBlocal#MDBlocal Goals for this session At the end of this session, you should be able to: ● Explain the document model ● Explain the difference between relational DBs and document-based DBs ● Understand how schema design impacts performance ● Think about schema design in a new way
  6. #MDBlocal#MDBlocal High-Level Schema Design Start with the Consumer
  7. #MDBlocal#MDBlocal Relational Schemas
  8. #MDBlocal#MDBlocal MongoDB Schemas
  9. #MDBlocal#MDBlocal How is Data Stored in MongoDB?
  10. #MDBlocal#MDBlocal MongoDB uses JSON Documents... Why JSON? JSON is: ● a widely-used standard ● fast, lean, flexible ● supported by almost all languages and tools ● used for modern communication & data exchange ...and stores them as binary JSON (BSON)
  11. #MDBlocal#MDBlocal MongoDB Documents are Rich Data Structures { first_name: ‘Paul’, surname: ‘Miller’, cell: 447557505611, city: ‘London’, location: { type: Point, coordinates: [-0.223,51.52]}, Profession: [‘banking’, ‘finance’, ‘trader’], cars: [ { model: ‘Bentley’, year: 1973, value: 100000, … }, { model: ‘Rolls Royce’, year: 1965, value: 330000, … } ] } Fields can contain an array of sub- documents (JSON objects) Typed field names Fields Array
  12. #MDBlocal#MDBlocal Document Schemas are Flexible { sku: ‘PAINTZXC123’, product_name: ‘Metallic Paint’, product_type: ‘paint’, colors: [‘Red’, ‘Green’], size_gallons: [5, 10] } { sku: ‘TSHRTASD43546’, product_name: ‘T-shirt’, product_type: ‘Mongodb swag’, colors: [‘Heather Gray’ … ], size: [‘S’, ‘M’, ‘L’, ‘XL’], material: ‘100% cotton’, wash: ‘cold’, dry: ‘tumble dry low’ } ● Different Documents in the same ProductsCatalog collection in MongoDB ● Polymorphic Schema - think base class & derived classes
  13. #MDBlocal#MDBlocal Progress on Goals for this session At the end of this session, you should be able to: ● Explain the document model ● Explain the difference between relational DBs and document-based DBs ● Understand how schema design impacts performance ● Think about schema design in a new way
  14. #MDBlocal#MDBlocal Schema Design Considerations and Patterns
  15. #MDBlocal#MDBlocal Factors to Consider ● How will this data be accessed? ● Number of Reads vs. Updates ● How big will each document likely be? And remember: Up-front schema design is critical! Nearly all performance issues stem from poor schema design!
  16. #MDBlocal#MDBlocal 2 Big “Rules”: ● Consider your consumers: how will this data be used? ● Data that works together lives together ○ Data Locality is one reason why MongoDB is so fast
  17. #MDBlocal#MDBlocal Example > Guitar Collectors ERD Relational DB MongoDB Table  Collection Column  Field Row  Document Terminology
  18. #MDBlocal#MDBlocal Example > Guitar Collectors Relational Consider your consumers: how will this data be used?
  19. #MDBlocal#MDBlocal Example > Guitar Collectors MongoDB - Embedding
  20. #MDBlocal#MDBlocal Example > Guitar Collectors Typical queries from a consumer of this data might be: ● “What guitars does Aimee Doe own?” ● “Show me all the people who own a Gibson Les Paul of any age” ● “How many people within 100 miles of Seattle own guitars made before 1970?” Consider how the data will be used X
  21. #MDBlocal#MDBlocal Example > Guitar Collectors Typical queries from a consumer of this data might be: ● “What guitars does Aimee Doe own?” ● “Show me all the people who own a Gibson Les Paul of any age” ● “How many people within 100 miles of Seattle own guitars made before 1970?” Consider how the data will be used
  22. #MDBlocal#MDBlocal Progress on Goals for this session At the end of this session, you should be able to: ● Explain the document model ● Explain the difference between relational DBs and document-based DBs ● Understand how schema design impacts performance ● Think about schema design in a new way
  23. #MDBlocal#MDBlocal Schema Design Patterns in MongoDB
  24. #MDBlocal#MDBlocal Design Patterns Data Relationships ● One : One ● One : Many ● Many : Many Learn More Here https://docs.mongodb.com/manual/ applications/data-models/ MongoDB Patterns ● Embedding ● Reference ● Subset ● Time Series ● Tree Structure
  25. #MDBlocal#MDBlocal Design Patterns: Real-World Examples
  26. #MDBlocal#MDBlocal Example > Healthcare We want to build a mobile app for patients and providers. Within our medical system: ● Patients have multiple medical procedures ● Doctors work at several hospitals & clinics, and vice versa
  27. #MDBlocal#MDBlocal 1:1 Relationships ← Embed – weak entity Medical Procedures Data that works together lives together.
  28. #MDBlocal#MDBlocal One:Many Relationships { _id: 2, first: “Joe”, last: “Patient”, addr: { …}, procedures: [ { id: 12345, date: 2015-02-15, type: “CAT scan”, …}, { id: 12346, date: 2015-02-15, type: “blood test”, …}] } Patients Embed Modeled in 2 possible ways: { _id: 2, first: “Joe”, last: “Patient”, addr: { …}, procedures: [12345, 12346] } { _id: 12345, date: 2015-02-15, type: “CAT scan”, …} { _id: 12346, date: 2015-02-15, type: “blood test”, …} Patients Referenc e Procedures OR
  29. #MDBlocal#MDBlocal Many : Many Relationship Like a One-to-Many relationship, you can embed sub-documents or reference them. Which approach you take depends on data access patterns and document sizes. The relational way: Hospitals ● Id ● Name ● Location ● Foo ● Bar Hospitals_to_Physicians Hospital_id Physician_id Physicians ● Id ● Name ● Address ● Phone ● etc “Who works at this hospital?” “At which clinics does my doctor work?”
  30. #MDBlocal#MDBlocal Many : Many Relationship using Embedding { _id: 1, name: “Oak Valley Hospital”, city: “New York”, beds: 131, physicians: [ { id: 12345, name: “Joe Doctor”, address: {…}, …}, { id: 12346, name: “Mary Well”, address: {…}, …}] } { _id: 2, name: “Plainmont Hospital”, city: “Omaha”, beds: 85, physicians: [ { id: 63633, name: “Harold Green”, address: {…}, …}, { id: 12345, name: “Joe Doctor”, address: {…}, …}] } Data Duplication is Fine! (usually)
  31. #MDBlocal#MDBlocal Many : Many Relationship using References { _id: 1, name: “Oak Valley Hospital”, city: “New York”, beds: 131, physicians: [12345, 12346] }, { _id: 2, name: “Plainmont Hospital”, city: “Omaha”, beds: 85, physicians: [63633, 12346] } { id: 63633, name: “Harold Green”, hospitals: [1,2], …}, { id: 12345, name: “Joe Doctor”, hospitals: [1], …}, { id: 12346, name: “Mary Well”, hospitals: [1,2], …} Hospitals Physicians Note that references can go either direction - there is no primary key/foreign key concept in MongoDB.
  32. #MDBlocal#MDBlocal Example > Movie Rental App We want to build a movie rental system. Our clients will want to: ● Search for movies by Title ● See the main cast members on the main movie page... ○ ...and be able to drill down for details ● Search for Actors ● See most recent movies for an actor on her main page This is a natural many : many relationship...but we don’t need all of the data.
  33. #MDBlocal#MDBlocal The Subset pattern • You want to display dependent information, but only part of it. • The rest of the data is fetched only if needed. • Examples: ‒ The main cast of a movie ‒ 5 most-recent movies an actor has starred in
  34. #MDBlocal#MDBlocal Example > Movie Rental App ● Search for movies by Title ● See the main cast members on the main movie page... ○ ...and be able to drill down for details ● Search for Actors ● See most recent movies for an actor on her main page Consider your consumers: how will this data be used?
  35. #MDBlocal#MDBlocal RECAP
  36. #MDBlocal#MDBlocal Progress on Goals for this session At the end of this session, you should be able to: ● Explain the document model ● Explain the difference between relational DBs and document-based DBs ● Understand how schema design impacts performance ● Think about schema design in a new way
  37. #MDBlocal#MDBlocal Data Governance: Constrain Your Schema
  38. #MDBlocal#MDBlocal Rich Indexes db.collectors.createIndex({ "location":"2dsphere", "cars.year": 1 });
  39. #MDBlocal#MDBlocal Expressive Queries
  40. #MDBlocal#MDBlocal Key Points ● Upfront schema design in MongoDB is important ● Differs from relational, but some concepts are still useful ● New patterns not possible in relational DBs ● Almost all performance issues are due to poor schema design
  41. #MDBlocal #MDBlocal THANK YOU FOR JOINING US!
別れさせ屋

http://artma.net.ua

best-cooler.reviews/

×