Database Reference
In-Depth Information
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 topics. 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>
In the nonrelational approach, the document might look something like the following:
{
"Type": "CD",
"Artist": "Nirvana",
"Title": "Nevermind",
"Genre": "Grunge",
"Releasedate": "1991.09.24",
"Tracklist": [
{
"Track" : "1",
"Title" : "Smells Like Teen Spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
}
 
Search WWH ::




Custom Search