Database Reference
In-Depth Information
The asterisks mean that the last five datatypes (date, object ID, binary data, regex,
and JavaScript code) are non-JSON types; specifically, they are special datatypes that
BSON allows you to use. In Chapter 4, you will learn how to identify your datatypes by
using the $type operator.
In theory, this all probably sounds straightforward. However, you might wonder how
you go about actually designing the document, including what information to put in it.
Because a document can contain any type of data, you might think there is no need to
reference information from inside another document. In the next section, we'll look at
the pros and cons of embedding information in a document compared to referencing that
information from another document.
Embedding vs. Referencing Information in Documents
You can choose either to embed information into a document or reference that
information from another document. Embedding information simply means that
you place a certain type of data (for example, an array containing more data) into the
document itself. Referencing information means that you create a reference to another
document that contains that specific data. Typically, you reference information when you
use a relational database. For example, assume you wanted to use a relational database
to keep track of your CDs, DVDs, and books. In this database, you might have one table
for your CD collection and another table that stores the track lists of your CDs. Thus, you
would probably need to query multiple tables to acquire a list of tracks from a specific CD.
With MongoDB (and other nonrelational databases), however, it would be much
easier to embed such information instead. After all, the documents are natively capable
of doing so. Adopting this approach keeps your database nice and tidy, ensures that all
related information is kept in one single document, and even works much faster because
the data is then co-located on the disk.
Now let's look at the differences between embedding and referencing information by
looking at a real-world scenario: storing CD data in a database.
In the relational approach, your data structure might look something like this:
|_media
|_cds
|_id, artist, title, genre, releasedate
|_ cd_tracklists
|_cd_id, songtitle, length
In the nonrelational approach, your data structure might look something like this:
|_media
|_items
|_<document>
 
Search WWH ::




Custom Search