Transcript

Schema Design

Technical Writer, MongoDB, Inc.

bob@mongodb.com

Bob Grabar

Agenda

• Core Concepts

• Modeling Data

• Modeling Relationships

Schema design is aboutyour Application

Relational Normalize

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

Terminology

RDBMS MongoDB

Table Collection

Row Document

Index Index

Join Embedded Document

Foreign Key Reference

Relational Record

• Two-dimensional storage

• Field can contain single value

• Structured schema

• Poor data locality

primarykey

MongoDB Document

_id• Multi-dimensional storage

• Field can contain many values

• Flexible schema

• Optimal data locality

BSON (Binary JSON)

Relational:Focus on data storage

MongoDB:Focus on data use

Relational:What answers do I have?

MongoDB:What questions do I have?

Modeling Data

Business Card

Contact Addre

ss

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}

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"

}}

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 : "larry@google.com", address : { street : "555 Bryant, #106", city : "Palo Alto", state : "CA", zip_code : "94301" } phone : "650-618-1499", fax : "650-330-0100"}

{ _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"}

Modeling Relationships

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

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

contact• twitter_id

twitter1 1

Contact• twitter

twitter 1

Schema Design Choices

One-to-One

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"

}

}

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

contact• phone_ids: [

]phone1 N

Contact• phones

phoneN

Schema Design Choices

One-to-Many

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

]

}

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

Contacts• name• company• title• phone

Groups• name

GroupContacts

• group_id• contact_id

Use arrays instead

XTraditional Relational Association

Join Table

Many-to-Many

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

groupcontact

• group_ids: [ ]

N N

General Recommendation

Many-to-Many

Contact referencesgroups

Many-to-Many

Group embeds contacts for performance

group• contacts

contactN

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

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” : “gary.murakami@mongodb.com” },

{ “type” : “home”, “address” : “gjm@nobell.org” }

]

}

Resources

mongodb.org/downloads

mongodb.com

Thank You

Technical Writer, MongoDB, Inc.

bob@mongodb.com

Bob Grabar

top related