Top Banner
Schema Design Technical Writer, MongoDB, Inc. [email protected] Bob Grabar
38
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Schema Design

Schema Design

Technical Writer, MongoDB, Inc.

[email protected]

Bob Grabar

Page 2: Schema Design

Agenda

• Core Concepts

• Modeling Data

• Modeling Relationships

Page 3: Schema Design

Schema design is aboutyour Application

Page 4: Schema Design

Relational Normalize

Page 5: Schema Design

MongoDB { "patient_id": "1177099", "first_name": "John", "last_name": "Doe", "dob": "2000-01-25", "gender": "Male", "blood_type": "B+", "address": "123 Elm St., Chicago, IL 59923", "height": "66", "weight": "110", "allergies": ["Nuts", "Penicillin", "Pet Dander"], "current_medications": [{"name": "Zoloft", "dosage": "2mg", "frequency": "daily", "route": "orally"}], "complaint" : [{"entered": "2000-11-03", "onset": "2000-11-03", "prob_desc": "", "icd" : 250.00, "status" : "Active"}, {"entered": "2000-02-04", "onset": "2000-02-04", "prob_desc": "in spite of regular exercise...", "icd" : 401.9, "status" : "Active"}], "diagnosis" : [{"visit" : "2005-07-22" , "narrative" : "Fractured femur", "icd" : "9999", "priority" : "Primary"}, {"visit" : "2005-07-22" , "narrative" : "Type II Diabetes", "icd" : "250.00", "priority" : "Secondary"}] }

Page 6: Schema Design

Terminology

RDBMS MongoDB

Table Collection

Row Document

Index Index

Join Embedded Document

Foreign Key Reference

Page 7: Schema Design

Relational Record

• Two-dimensional storage

• Field can contain single value

• Structured schema

• Poor data locality

primarykey

Page 8: Schema Design

MongoDB Document

_id• Multi-dimensional storage

• Field can contain many values

• Flexible schema

• Optimal data locality

Page 9: Schema Design

BSON (Binary JSON)

Page 10: Schema Design

Relational:Focus on data storage

MongoDB:Focus on data use

Page 11: Schema Design

Relational:What answers do I have?

MongoDB:What questions do I have?

Page 12: Schema Design

Modeling Data

Page 13: Schema Design

Business Card

Page 14: Schema Design

Contact Addre

ss

Page 15: Schema Design

Referencing

Addresses

{_id : 1,street : "10260 Bandley Dr",city : "Cupertino",state : "CA",zip_code : "95014",country : "USA"

}

Contacts

{ _id : 2, name : "Steven Jobs", title : "VP, New Product Development", company : "Apple Computer", phone : "408-996-1010", address_id : 1}

Page 16: Schema Design

Embedding

Contacts

{ _id : 2, name : "Steven Jobs", title : "VP, New Product Development", company : "Apple Computer", phone : "408-996-1010", address : {

street : "10260 Bandley Dr",city : "Cupertino",state : "CA",zip_code : "95014",country : "USA"

}}

Page 17: Schema Design

Schema Flexibility

Contacts

{ name : "Steven Jobs", title : "VP, New Product Development", company : "Apple Computer", phone : "408-996-1010" address : {

street : "10260 Bandley Dr",city : "Cupertino",state : "CA",zip_code : "95014"

}}

{ name : "Larry Page", url : "http://google.com/", title : "CEO", company : "Google!", email : "[email protected]", address : { street : "555 Bryant, #106", city : "Palo Alto", state : "CA", zip_code : "94301" } phone : "650-618-1499", fax : "650-330-0100"}

Page 18: Schema Design

{ _id : 2, name : “Steven Jobs", title : "VP, New Product Development", company : "Apple Computer", address : [

{ street : "10260 Bandley Dr", city : "Cupertino", state : "CA", zip_code : "95014", country : "USA"

},{ street : "2066 Crist Drive", city : "Los Altos", state : "CA"

zip_code : "94024" country : "USA"

} ], phone : "408-996-1010"}

Schema Flexibility

{ _id : 2, name : "Steven Jobs", title : "VP, New Product Development", company : "Apple Computer", address : {

street : "10260 Bandley Dr",city : "Cupertino",state : "CA",zip_code : "95014",country : "USA"

}, phone : "408-996-1010"}

Page 19: Schema Design

Modeling Relationships

Page 20: Schema Design

Address Book Entity-Relationship

Contacts• name• company• title

Addresses

• type• street• city• state• zip_code

Phones• type• number

Emails• type• address

Thumbnails

• mime_type• data

Portraits• mime_type• data

Groups• name

N

1

N

1

N

N

N

1

1

1

11

Twitters• name• location• web• bio

1

1

Page 21: Schema Design

One-to-One

Contacts• name• company• title

Addresses

• type• street• city• state• zip_code

Phones• type• number

Emails• type• address

Thumbnails

• mime_type• data

Portraits• mime_type• data

Groups• name

N

1

N

1

N

N

N

1

1

1

11

Twitters• name• location• web• bio

1

1

Page 22: Schema Design

contact• twitter_id

twitter1 1

Contact• twitter

twitter 1

Schema Design Choices

One-to-One

Page 23: Schema Design

Contact• twitter

twitter 1

db.contacts.find( { "twitter.location" : "Certaldo" } )

General Recommendation

One-to-One

{

_id : ObjectId("52ebde4e5f2b124a3a09eb8e"),

name : "Giovanni Boccaccio",

twitter : {

name : "Gian",

location : "Certaldo",

web : "https://twitter.com/RealBoccaccio"

}

}

Page 24: Schema Design

One-to-Many

Contacts• name• company• title

Addresses

• type• street• city• state• zip_code

Phones• type• number

Emails• type• address

Thumbnails

• mime_type• data

Portraits• mime_type• data

Groups• name

N

1

N

1

N

N

N

1

1

1

11

Twitters• name• location• web• bio

1

1

Page 25: Schema Design

contact• phone_ids: [

]phone1 N

Contact• phones

phoneN

Schema Design Choices

One-to-Many

Page 26: Schema Design

Contact• phones

phoneN

Contact Book Application

One-to-Many

{

_id : ObjectId("52ebde4e5f2b124a3a09eb8e"),

name : "Giovanni Boccaccio",

twitter : {

name : "Gian",

location : "Certaldo",

web : "https://twitter.com/RealBoccaccio"

},

phones : [

{ type : "work" , number : "+39 0571-669811" },

{ type : "home" , number : "+39 671-946726" },

{ type : "mobile" , number : "+39 671-038747" }

]

}

Page 27: Schema Design

Contacts• name• company• title

Addresses

• type• street• city• state• zip_code

Phones• type• number

Emails• type• address

Thumbnails

• mime_type• data

Portraits• mime_type• data

Groups• name

N

1

N

1

N

N

N

1

1

1

11

Twitters• name• location• web• bio

1

1

Many-to-Many

Page 28: Schema Design

Contacts• name• company• title• phone

Groups• name

GroupContacts

• group_id• contact_id

Use arrays instead

XTraditional Relational Association

Join Table

Many-to-Many

Page 29: Schema Design

group

• contact_ids: [ ]

contactN N

groupcontact

• group_ids: [ ]

N N

group• contacts

contactN

contact• groups

group N

Schema Design Choices

Many-to-Many

Reference Embed

Page 30: Schema Design

groupcontact

• group_ids: [ ]

N N

General Recommendation

Many-to-Many

Contact referencesgroups

Page 31: Schema Design

Many-to-Many

Group embeds contacts for performance

group• contacts

contactN

Page 32: Schema Design

Contacts• name• company• title

addresses• type• street• city• state• zip_code

phones• type• number

emails• type• address

thumbnail• mime_type• data

Portraits• mime_type• data

Groups• name

N

1

N

1

twitter• name• location• web• bio

N

N

N

1

1

Document model - holistic and efficient representation

Page 33: Schema Design

Contact document example

{

“name” : “Gary J. Murakami, Ph.D.”,

“company” : “MongoDB, Inc.”,

“title” : “Lead Engineer”,

“twitter” : {

“name” : “Gary Murakami”, “location” : “New Providence, NJ”,

“web” : “http://www.nobell.org”

},

“portrait_id” : 1,

“addresses” : [

{ “type” : “work”, “street” : ”229 W 43rd St.”, “city” : “New York”, “zip_code” : “10036” }

],

“phones” : [

{ “type” : “work”, “number” : “1-866-237-8815 x8015” }

],

“emails” : [

{ “type” : “work”, “address” : “[email protected]” },

{ “type” : “home”, “address” : “[email protected]” }

]

}

Page 34: Schema Design

Resources

Page 35: Schema Design

mongodb.org/downloads

Page 36: Schema Design
Page 37: Schema Design

mongodb.com

Page 38: Schema Design

Thank You

Technical Writer, MongoDB, Inc.

[email protected]

Bob Grabar