Database Reference
In-Depth Information
Chapter 1. To Embed or Reference
When building a new application, often one of the first things you'll want to do is to design
its data model. In relational databases such as MySQL, this step is formalized in the process
of normalization, focused on removing redundancy from a set of tables. MongoDB, unlike re-
lational databases, stores its data in structured documents rather than the fixed tables required
in relational databases. For instance, relational tables typically require each row-column inter-
section tocontain asingle, scalar value. MongoDBBSONdocuments allow formorecomplex
structure by supporting arrays of values (where each array itself may be composed of multiple
subdocuments).
This chapter explores one of the options that MongoDB's rich document model leaves open
to you: the question of whether you should embed related objects within one another or ref-
erence them by ID. Here, you'll learn how to weigh performance, flexibility, and complexity
against one another as you make this decision.
Relational Data Modeling and Normalization
BeforejumpingintoMongoDB'sapproachtothequestionofembeddingdocumentsorlinking
documents, we'll take a little detour into how you model certain types of relationships in rela-
tional (SQL) databases. In relational databases, data modeling typically progresses by model-
ing your data as a series of tables , consisting of rows and columns , which collectively define
the schema of your data. Relational database theory has defined a number of ways of putting
application data into tables, referred to as normal forms . Although a detailed discussion of re-
lational modeling goes beyond the scope of this text, there are two forms that are of particular
interest to us here: first normal form and third normal form.
What Is a Normal Form, Anyway?
Schema normalization typically begins by putting your application data into the first normal
form (1NF). Although there are specific rules that define exactly what 1NF means, that's a
little beyond what we want to cover here. For our purposes, we can consider 1NF data to be
any data that's tabular (composed of rows and columns), with each row-column intersection
Search WWH ::




Custom Search