Embedding vs Referencing Information in documents

Image

Embedding vs. Referencing Information in Documents
From The Definitive Guide to MongoDB (Plugge, Membrey & Hawkins)

You can choose either to embed information into a document or reference that information in another document. Embedding information simply means that you place a certain type of data (e.g., an array containing more data) into the document itself.

Referencing information simply means that you create a reference back to another document that contains that specific data.

Typically, you reference information when you use a relational database.

If you used an RDBMS to model your CDs, DVDs and books collection, you’d probably  have one table for your CD collection and another table that stores the tracklists of your CDs. You’d need to join across >1 table to get a list of tracks from a specific CD.

With MongoDB , however, it would be much easier to embed such information instead. This 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.
In the relational approach, your data structure might look something like this:

>media

tbl_cds

[columns] id, artist, title, genre, releasedate

tbl_cd_tracklists

[columns] cd_id, songtitle, length

In the non-relational approach, your data structure might look something like this:

>media

>items

<document>

In the noSQL approach, the document might look something like the following:
{
“Type”: “CD”,
“Artist”: “One Direction”,
“Title”: “Nevermind”,
“Genre”: “Kids!!!”,
“Release date”: “dd.mm.yyyy”,
“Tracklist”: [

{
“Track” : “1”,
“Title” : “Live While We’re Young”,
“Length” : “3:20”
},

{
“Track” : “2”,
“Title” : “Kiss You”,
“Length” : “3:03”
},

{
“Track” : “3”,
“Title” : “Unknown”,
“Length” : “m:ss”
}

    ]
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s