<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>theChrisWalker.net &#187; Rapid Application Development</title>
	<atom:link href="http://thechriswalker.net/tag/rad/feed" rel="self" type="application/rss+xml" />
	<link>http://thechriswalker.net</link>
	<description>I like Web Development</description>
	<lastBuildDate>Tue, 31 Aug 2010 07:42:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Mongo &#8212; and the coolness of Document-Oriented Databases</title>
		<link>http://thechriswalker.net/2010-03/mongo-and-the-coolness-of-document-oriented-databases.html</link>
		<comments>http://thechriswalker.net/2010-03/mongo-and-the-coolness-of-document-oriented-databases.html#comments</comments>
		<pubDate>Tue, 09 Mar 2010 21:44:22 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Rapid Application Development]]></category>

		<guid isPermaLink="false">http://thechriswalker.net/?p=173</guid>
		<description><![CDATA[I have been following a PHP Rapid Application Development Framework called Lithium with much interest (for other reasons which I fully intend to blog about later) and they are the ones I owe for turning me on to document oriented database systems. So what are they and why are they so cool, and what/who the [...]]]></description>
			<content:encoded><![CDATA[<p>I have been following a PHP Rapid Application Development Framework called <a href="http://lithify.me" title="The Most RAD PHP Framework: Lithium">Lithium</a> with much interest (for other reasons which I fully intend to blog about later) and they are the ones I owe for turning me on to document oriented database systems. </p>
<p>So what are they and why are they so cool, and what/who the hell is &#8220;Mongo&#8221;?</p>
<p>Let&#8217;s deal with the what first. &#8220;Traditional&#8221; database systems are Relational, they are characterised by strictly defined tables, strong relations between tables and their <a href="http://wikipedia.org/wiki/ACID" title="ACID on Wikipedia">ACID</a> compliance. It simple terms that means you say: </p>
<p><em>&#8220;I want a table of users and each one <strong>will</strong> have a first name, a last name and an email address, and each of those <strong>will</strong> be a &#8216;text&#8217; entry of a maximum of 255 characters&#8221;</em>.</p>
<p>If at a later date you want to add more info, then you have to go back and alter your table definition, give the old rows default values and fix any coding bugs that relied on the previous structure (not that you&#8217;d ever develop such a dependant application). Now the approach in a document oriented database is that you say:</p>
<p><em>&#8220;I want a table of users&#8221;</em></p>
<p>Then you can give each one whatever data you want. you may have one user with a name and address, another with a name and phone number only, a third might have a phone number and address but no first name. All these records can co-exist! That&#8217;s pretty different and I really didn&#8217;t know whether it was a good idea. So I played with <a href="http://mogodb.org/" title="MongoDB homepage">MongoDB</a>.</p>
<p><span id="more-173"></span></p>
<p>MongoDB is really cool. I installed it my laptop simply and fired up the shell to play. The syntax is very javascript-like, becasue it <em>is</em> javascript! So we can do this.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MongoDB shell version<span style="color: #339933;">:</span> 1.3.3<span style="color: #339933;">-</span><br />
url<span style="color: #339933;">:</span> test<br />
connecting to<span style="color: #339933;">:</span> test<br />
type <span style="color: #3366CC;">&quot;help&quot;</span> <span style="color: #000066; font-weight: bold;">for</span> help<br />
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">myFirstMongoDB</span>.<span style="color: #660066;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>key<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> mixed<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;one&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;three&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> deep<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>has<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>another<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4b96b836ea70a136feb89c20&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">myFirstMongoDB</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4b96b836ea70a136feb89c20&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;key&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #3366CC;">&quot;mixed&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">&quot;one&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;three&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp;<span style="color: #3366CC;">&quot;deep&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;has&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;another&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;object&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&gt;</span></div></div>
<p>Notice two amazing things here.</p>
<ol>
<li>We never created the table! we simply call <tt>db.myFirstMongoDB...</tt> which creates the &#8220;collection&#8221; (as they are called in the document oriented world). We certainly never defined a schema!</li>
<li>We passed an object with nested data. Not just simple fields! We can use this in a much more useful way in the next example</li>
</ol>
<p>So why might you want to do this. Consider a website with articles. Articles are written by a person, they are published on a date and the have content, tags, comments, view counts, trackbacks, and a proprietary rating system. If this where a relational database, we are looking at the following tables:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">articles: holds the article, date, person_id, view counts, proprietary rating<br />
people: holds the writers<br />
comments: the comments, related by article_id<br />
trackbacks: link back details<br />
ratings: the info for the proprietary system.<br />
tags: tag info<br />
tags_articles: a join table linking tags to the articles</div></div>
<p>Joining all that info together is not only complex, but can be a fair amount of load on the system. As more info wants to be added by the website owners changes become more complicated and difficult. However with Mongo I can hold the article in one collection like this:</p>
<p>[</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span> <br />
&nbsp; <span style="color: #3366CC;">&quot;published&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2010-03-09 21:21:00&quot;</span><span style="color: #339933;">,</span> <br />
&nbsp; <span style="color: #3366CC;">&quot;author&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Chris Walker&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;profile&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/profiles/chris-walker&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;My amazing Article&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">&quot;content&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;here's the text...&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">&quot;tags&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">&quot;wonder&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;amazement&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;mongo&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;demo&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;chris&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">&quot;comments&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;commenter&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Rosie&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;comment&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;What is this?&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;when&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2010-03-09 22:02:00&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;commenter&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Chris&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://thechriswalker.net/&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;comment&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;it's class, that's what!&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;when&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2010-03-09 23:12:00&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;commenter&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;anonymous&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;comment&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;I am your father.&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;when&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2010-03-10 01:25:20&quot;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; rating<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> stars<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> tagline<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;what a monster!&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; views<span style="color: #339933;">:</span> <span style="color: #CC0000;">1024</span><span style="color: #339933;">,</span><br />
&nbsp; trackbacks<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://some.blog/some/where&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;quote&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;the text for the link&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://some.other.blog/some/where/else&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;quote&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;the text they used to link here&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>So that might look like a more complex entity. It is, but you can search for values in sub-documents, you can filter results on existence of certain keys, and you can pull all this info one result. Your previous 7 tables and 8 joins, just became 1 collection and no joins. guess which is quicker?</p>
<p>Try to pull articles with a certain tag: &#8220;balls&#8221;? In SQL you&#8217;d be doing this:</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`article_id`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`tags_articles`</span> <span style="color: #ff0000;">`ta`</span> <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #ff0000;">`tags`</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #ff0000;">`ta`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`tag_id`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">`t`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`t`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`name`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'balls'</span></div></div>
<p>And that would just get you the list of Id&#8217;s and you&#8217;d still need to get the info for each article. In Mongo with the collection we described above, you&#8217;d do:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">db.<span style="color: #660066;">articles</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> tags<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;balls&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></div>
<p>How much easier is that, and we&#8217;ve now got all the info, not just the &#8220;id&#8221;s! I was just amazed at how easy that is!</p>
<p>Of course this simplicity comes with a price, you lose the relational elements making tight relationships harder to work with. Also you some ease in aggregating things like &#8220;tags&#8221;, but MapReduce support makes this very efficient anyway.</p>
<p>In my next blog I&#8217;ll talk about Lithium, Mongo and how fast you can build a full blown web app with these tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://thechriswalker.net/2010-03/mongo-and-the-coolness-of-document-oriented-databases.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
