Database Reference
In-Depth Information
posts
int(11)
int(11)
varchar(255)
text
smallint(5)
id
author_id
title
url
vote_count
posts_tags
id int(11)
post_id int(11)
tag_id int(11)
comments
int(11)
int(11)
int(11)
text
id
post_id
user_id
text
tags
id int(11)
text varchar(255)
images
int(11)
int(11)
int(11)
varchar(255)
mediumint(8)
varchar(255)
id
post_id
caption
type
size
location
Figure 1.1 A basic relational data model
for entries on a social news site
You've probably noticed that in addition to providing a richness of structure, docu-
ments need not conform to a prespecified schema. With a relational database, you
store rows in a table. Each table has a strictly defined schema specifying which col-
umns and types are permitted. If any row in a table needs an extra field, you have to
alter the table explicitly. MongoDB groups documents into collections, containers
that don't impose any sort of schema. In theory, each document in a collection can
have a completely different structure; in practice, a collection's documents will be rel-
atively uniform. For instance, every document in the posts collection will have fields
for the title, tags, comments, and so forth.
But this lack of imposed schema confers some advantages. First, your application
code, and not the database, enforces the data's structure. This can speed up initial
application development when the schema is changing frequently. Second, and more
significantly, a schemaless model allows you to represent data with truly variable prop-
erties. For example, imagine you're building an e-commerce product catalog. There's
no way of knowing in advance what attributes a product will have, so the application
will need to account for that variability. The traditional way of handling this in a fixed-
schema database is to use the entity-attribute-value pattern, 3 shown in figure 1.2. What
you're seeing is one section of the data model for Magento, an open source
e-commerce framework. Note the series of tables that are all essentially the same,
except for a single attribute, value , that varies only by data type. This structure allows
3
http://en.wikipedia.org/wiki/Entity-attribute-value_model
 
Search WWH ::




Custom Search