This document discusses schema design in MongoDB. It begins with an agenda covering core concepts, modeling data, and modeling relationships. It then discusses how MongoDB documents allow for flexible, optimal schemas compared to relational databases. Examples are provided for embedding and referencing data in one-to-one, one-to-many, and many-to-many relationships. The document recommends modeling embedded data when performance is important and referencing data otherwise. Finally, resources for learning more about MongoDB are provided.
16. Embedding
Contacts
{
_id : ,
name :
title :
company :
phone :
address : {
street :
city :
state :
,
zip_code :
country :
}
}
,
,
",
,
,
,
,
17. Schema Flexibility
Contacts
{
name :
url :
title :
,
company :
email :
address : {
street :
city :
state :
,
zip_code :
}
phone :
fax
{
name :
title :
company :
phone :
address : {
street :
city :
state :
,
zip_code :
}
,
,
,
,
,
}
}
,
,
,
,
,
,
,
18. Schema Flexibility
{
{
_id : ,
name :
title :
company :
address : {
street :
city :
state :
,
zip_code :
country :
},
phone :
_id : ,
,
name :
title :
company :
address : [
{
street :
city :
state :
,
zip_code :
country :
},
{
street :
city :
state :
zip_code :
country :
}
],
phone :
,
,
,
,
,
}
}
,
,
,
,
,
,
,
,
20. Address Book Entity-Relationship
•
•
•
•
name
location
web
bio
• name
N
1
N
1
1
Contacts
Thumbnails
• mime_type
• data
1
•
•
•
N •
•
type
street
city
state
zip_code
Phones
• name
1 • company
• title
1
1
1
Portraits
• mime_type
• data
Addresses
Groups
Twitters
1
N • type
• number
Emails
N • type
• address
32. Document model - holistic and efficient representation
Groups
Contacts
• name
N
• name
• company
• title
twitter
N
1
1
Portraits
• mime_type
• data
•
•
•
•
addresses N
1
name
location
web
bio
thumbnail 1
• mime_type
• data
•
•
•
•
•
type
street
city
state
zip_code
phones
N
• type
• number
emails
• type
• address
N