12. 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
13. 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
14. 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
26. 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
Twitter
• name
• location
• web
• bio
1
1
27. 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
Twitter
• name
• location
• web
• bio
1
1
29. 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"
}
}
30. 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
31. contact
• phone_ids: [ ]
phone1 N
Contact
• phones
phone N
Schema Design Choices
One-to-Many
32. 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"
]
}
33. 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
34. Contacts
• name
• company
• title
• phone
Groups
• name
GroupContacts
• group_id
• contact_id
Use arrays instead
X
Traditional Relational Association
Join Table
Many-to-Many
35. group
• contact_ids: [ ]
contactN 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
36. group contact
• group_ids: [ ]N N
Many-to-Many
• Use case: address
book
• Contact references
groups
Reference
38. 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