Top Banner
Schema Design Sr. Solutions Architect, MongoDB, Inc. Jake Angerman
43

MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Apr 21, 2017

Download

Data & Analytics

MongoDB
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: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Schema Design

Sr. Solutions Architect, MongoDB, Inc. Jake Angerman

Page 2: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Agenda

•  Traditional Relational Model

•  MongoDB Document Model

•  Associating Entities in MongoDB

•  There will be a test at the end

Page 3: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Relational Model

Page 4: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Relational Model This is hard…

Long time to develop

Queries are complex

Difficult to change

Page 5: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

What happens when the requirements change?

Page 6: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Hard to make changes New Table

New Table

New Column

Name Age Phone Email

New Column

Page 7: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Have to Manage Changes in 3 Places

Application

Code

Relational Database

Object Relational Mapping

XML Config DB Schema

Page 8: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Relational Model

Page 9: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Developers can be more productive

Application

Code

Page 10: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Developers can be more productive

Application

Code

Rich Queries Geospatial

Text Search

Map Reduce

Aggregation

Page 11: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

The Document Model

Page 12: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Key → Value store

•  One-dimensional storage

•  Single value is a blob

•  Query on key only

•  No schema

•  Value can be replaced but not updated

Key Blob

Page 13: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Relational Record

•  Two-dimensional storage

•  Field contains a single value

•  Query on any field

•  Very structured schema

•  Poor data locality requires many tables, joins, and indexes.

Primary Key

Page 14: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

MongoDB Document

_id •  N-dimensional storage

•  Field can contain many values and embedded values

•  Query on any field & level

•  Flexible schema

•  Optimal data locality requires fewer indexes and provides better performance

Page 15: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Documents are easier

Relational MongoDB { ! first_name: "Paul",! surname: "Miller"! city: "London",! location: [45.123,47.232],! cars: [ ! { model: "Bentley",! year: 1973,! value: 100000, … },! { model: "Rolls Royce",! year: 1965,! value: 330000, … }! }!}!

Page 16: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Terminology

MongoDB RDBMS

Document Row

Collection Table

Index Index

Embedded Document Join

Reference Foreign Key

Page 17: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Focus on data storage

Focus on data use

Page 18: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

What answers do I have?

What questions do I have?

Page 19: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Modeling Data with MongoDB business card example

Page 20: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Business Card

Contact

Address

Page 21: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Referencing

Addresses {

_id : , street : , city : , state : ”, zip_code : , country :

}

Contacts { _id : , name : , title : , company : ”, phone : , address_id : }

Page 22: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Embedding

Contacts { _id : , name : , title : , company : , address : {

street : , city : , state : , zip_code : , country :

}, phone : }

Page 23: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Schema Flexibility – different shapes

{ _id: , name : , title : , company : , address : {

street : , city : , state : , zip_code :

}, phone : }

{ _id : , name : , url : , title : , company : , email : , address : { street : , city : , state : , zip_code : } phone : , fax }

Page 24: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

{ “_id”: , “name”: , “title”: , “company”: , “address”: [

{ “street”: , “city”: , “state”: , “zip_code”: , “country”:

}, { “street”: , “city”: , “state”:

} ], “phone”: }

Schema Flexibility – easily changed

{ _id : , name : , title : , company : , address : {

street : , city : , state : , zip_code : , country :

}, phone : }

Page 25: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Modeling Data with MongoDB address book example

Page 26: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

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

1 1

Twitter •  name •  location •  web •  bio

1

1

Page 27: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

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

1 1

Twitter •  name •  location •  web •  bio

1

1

Page 28: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

contact

•  twitter_id

twitter 1 1

Contact •  twitter

twitter 1

Schema Design Choices One-to-One

Page 29: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Contact •  twitter

twitter 1

You can query or index on embedded fields. For example:

db.contacts.find( { "twitter.account" : "@Decameron" } )

General Recommendation One-to-One

{ _id : 1375-12-21, name : "Giovanni Boccaccio", twitter : { account : "@Decameron", page : "https://twitter.com/RealBoccaccio" } }

Page 30: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

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

1 1

Twitters •  name •  location •  web •  bio

1

1

Page 31: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

contact

•  phone_ids: [ ]

phone 1 N

Contact •  phones

phone N

Schema Design Choices One-to-Many

Page 32: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Contact •  phones

phone N

General Recommendation One-to-Many Few

{ _id : 1375-12-21, name : "Giovanni Boccaccio", twitter : { account : "@Decameron", page : "https://twitter.com/RealBoccaccio" } phone : [ work : "+39 0571-669811", home : "+39 671-946726", mobile : "+39 671-038747" ] }

Page 33: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

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

1 1

Twitters •  name •  location •  web •  bio

1

1

Many-to-Many

Page 34: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Contacts •  name •  company •  title •  phone

Groups •  name

GroupContacts •  group_id •  contact_id

Use arrays instead

XTraditional Relational Association

Join Table

Many-to-Many

Page 35: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

group

•  contact_ids: [ ]

contact N N

group contact •  group_ids: [ ] N N

Duplicated data must be updated for consistency

group •  contacts

contact N

contact •  groups

group N

Schema Design Choices

Many-to-Many

Reference Embed

Page 36: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

group contact •  group_ids: [ ] N N

Many-to-Many

•  Use case: address book

•  Contact references groups

Reference

Page 37: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

group

•  contacts

contact N

Many-to-Many

•  Use case: corporate email system

•  Group embeds contacts for performance

Embed

Page 38: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

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 39: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

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” :

, “phones” :

, “emails” :

}

Page 40: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Free training at university.mongodb.com

Page 41: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

Download at mongodb.org/downloads

Page 42: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
Page 43: MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

25% off discount code: JakeAngerman