<?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>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; CouchDB</title>
	<atom:link href="http://elegantcode.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Buy Ketoconazole Cream Without Prescription</title>
		<link>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=view-collation-in-couchdb</link>
		<comments>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/02/14/view-collation-in-couchdb/</guid>
		<description><![CDATA[A couple of weeks ago I learned about a very neat feature of CouchDB called view collations Buy Ketoconazole Cream Without Prescription, . Purchase Ketoconazole Cream, Basically, view collations enables us to make joins between documents, after Ketoconazole Cream. Herbal Ketoconazole Cream, Let’s look at a simple example here. Suppose we have a customer-order model [...]]]></description>
			<content:encoded><![CDATA[<p> <p align="justify">A couple of weeks ago I learned about a very neat feature of CouchDB called <a href="http://wiki.apache.org/couchdb/View_collation" target="_blank">view collations</a> <b>Buy Ketoconazole Cream Without Prescription</b>, .  <b>Purchase Ketoconazole Cream</b>, Basically, view collations enables us to make joins between documents, <b>after Ketoconazole Cream</b>.  <b>Herbal Ketoconazole Cream</b>, Let’s look at a simple example here.</p>  <p align="justify">Suppose we have a <em>customer-order</em> model where a customer can have one or more orders. The data of the documents for our example looks like this:</p>  <pre style="width: 100%; height: 631px" class="csharpcode">{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>, <b>online buy Ketoconazole Cream without a prescription</b>, <b>Online buying Ketoconazole Cream hcl</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-2eae48beb08acc72ed2ab64ccfbcb1c7&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>, <b>buy generic Ketoconazole Cream</b>, <b>Buy Ketoconazole Cream online no prescription</b>, <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Homer&quot;</span>,   <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Simpson&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0013c2&quot;</span>, <b>Ketoconazole Cream no prescription</b>, <b>Fast shipping Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9fc81f3b250c514fbd7e1d5f4a44816b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream results</b>, <b>Online Ketoconazole Cream without a prescription</b>, <span class="str">&quot;amount&quot;</span>: 34,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a001b53&quot;</span>, <b>buy cheap Ketoconazole Cream</b>, <b>Effects of Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;2-9dbf739124353363d53d1ba6671aac4e&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>Ketoconazole Cream dose</b>, <b>Ketoconazole Cream photos</b>, <span class="str">&quot;amount&quot;</span>: 58,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a0007ba&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>, <b>Ketoconazole Cream maximum dosage</b>, <b>Cheap Ketoconazole Cream</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-5cb6d3729618eb7db0f9285237fd8308&quot;</span>,   <span class="str">&quot;firstName&quot;</span>: <span class="str">&quot;Ned&quot;</span>, <b>Ketoconazole Cream used for</b>, <b>Where to buy Ketoconazole Cream</b>, <span class="str">&quot;lastName&quot;</span>: <span class="str">&quot;Flanders&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;customer&quot;</span>}</p>
<p>{   <span class="str">&quot;_id&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002a4f&quot;</span>, <b>Ketoconazole Cream pics</b>, <b>Purchase Ketoconazole Cream online no prescription</b>, <span class="str">&quot;_rev&quot;</span>: <span class="str">&quot;1-cc5eb0d8c6db0f698c53f9078d16db3b&quot;</span>,   <span class="str">&quot;type&quot;</span>: <span class="str">&quot;order&quot;</span>, <b>order Ketoconazole Cream from United States pharmacy</b>, <b>Where can i cheapest Ketoconazole Cream online</b>, <span class="str">&quot;amount&quot;</span>: 75,   <span class="str">&quot;customer&quot;</span>: <span class="str">&quot;807023fe77d927e1c222adfe4a002516&quot;</span>}</pre><style type="text/css">.csharpcode, <b>Ketoconazole Cream without a prescription</b>, <b>Ketoconazole Cream canada, mexico, india</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>buying Ketoconazole Cream online over the counter</b>, <b>Where can i buy cheapest Ketoconazole Cream online</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">Here we have two customers with their respective orders, <b>online buying Ketoconazole Cream</b>.  <b>Order Ketoconazole Cream from mexican pharmacy</b>, The first customer has two orders and the second customer has only one order.  Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&#160; </p></p>
<p><p align="justify">The final goal is that we want to retrieve all customers with their corresponding orders using a single <a href="http://elegantcode.com/2009/07/10/views-into-couchdb/" target="_blank">view</a>, <b>Buy Ketoconazole Cream Without Prescription</b>. In order to accomplish this we have to make use of complex keys in our map function:</p></p>
<p><pre style="width: 100%; height: 136px" class="csharpcode"><span class="kwrd">function</span>(document) {  <span class="kwrd">if</span>(document.type == <span class="str">'customer'</span>) {    emit([document._id, <b>purchase Ketoconazole Cream for sale</b>, <b>Comprar en línea Ketoconazole Cream, comprar Ketoconazole Cream baratos</b>, 0], document);  }  <span class="kwrd">else</span> <span class="kwrd">if</span>(document.type == <span class="str">'order'</span>) {    emit([document.customer, <b>Ketoconazole Cream blogs</b>, <b>Buy cheap Ketoconazole Cream no rx</b>, 1], document);  }<br />
}</pre><style type="text/css">.csharpcode, <b>cheap Ketoconazole Cream no rx</b>, <b>Ketoconazole Cream gel, ointment, cream, pill, spray, continuous-release, extended-release</b>, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", <b>Ketoconazole Cream schedule</b>, <b>Ketoconazole Cream no rx</b>, courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt<br />
{	background-color: #f4f4f4;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></p>
<p><p align="justify">The key is composed of the ID of a customer and an&#160; arbitrary number used for sorting (collating), <b>Ketoconazole Cream steet value</b>.  <b>My Ketoconazole Cream experience</b>, For this number we’ve used 0 for customer documents and&#160; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, <b>Ketoconazole Cream long term</b>, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:</p></p>
<p><p align="justify"><a href="http://elegantcode.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2012/02/image_thumb1.png" width="600" height="165" /></a></p></p>
<p><p align="justify">Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.</p></p>
<p><p align="justify">We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the <em>startkey/endkey</em> query options in order to get a subset of the view rows:</p></p>
<p><blockquote>  <p align="left">?startkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;]&amp;endkey=[&quot;807023fe77d927e1c222adfe4a0007ba&quot;, 2]</p></blockquote></p>
<p><p align="justify">Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&#160; </p></p>
<p><p align="justify">Until next time</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4715'>Aldactone For Sale</a>. <a href='http://elegantcode.com/?p=4746'>Buy Clindamycin Gel Without Prescription</a>. <a href='http://elegantcode.com/?p=4328'>Buy Plavix Without Prescription</a>. <a href='http://elegantcode.com/?p=4302'>Buy Phenergan Without Prescription</a>. <a href='http://elegantcode.com/?p=4460'>Buy Proscar Without Prescription</a>. <a href='http://elegantcode.com/?p=4751'>Diflucan street price</a>. <a href='http://elegantcode.com/?p=4372'>Nexium steet value</a>. <a href='http://elegantcode.com/?p=4475'>Metronidazole Gel from mexico</a>. <a href='http://elegantcode.com/?p=4399'>Herbal Periactin</a>. <a href='http://elegantcode.com/?p=4858'>Tramadol over the counter</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.macneilbmx.com/blog/?p=6622'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.greatgreengoods.com/?p=2425'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=9183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://evanrapoport.com/?p=529'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3183'>Buy Ketoconazole Cream Without Prescription</a>. <a href='http://social-blend.com/?p=634'>Ketoconazole Cream recreational</a>. <a href='http://blog.farmland.org/?p=3770'>Ketoconazole Cream without a prescription</a>. <a href='http://4realz.net/?p=640'>Ketoconazole Cream maximum dosage</a>. <a href='http://reversemortgagedaily.com/?p=14591'>Buy cheap Ketoconazole Cream</a>. <a href='http://linuxologist.com/?p=569'>Is Ketoconazole Cream safe</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/02/14/view-collation-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>  <p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>  <blockquote>   <p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p> </blockquote>  <p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>  <blockquote>   <p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p> </blockquote>  <p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>  <p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Design Documents in CouchDB and Validation</title>
		<link>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-documents-in-couchdb-and-validation</link>
		<comments>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 23:14:30 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB Views into CouchDB I briefly mentioned 'design documents' in my previous post as [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB:  <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/views-into-couchdb/">Views into CouchDB</a></li></ol> <p>I briefly mentioned '<em>design documents</em>' in my previous post as the way views are being stored in CouchDB. This is probably the most common use of design documents, but there's more. One thing that's interesting is the ability of performing validation. Now before anyone starts raving like a mad man, I'm not implying that all validation or, to make matters even worse, that business rules should now all be handled by CouchDB, let along they should all be written in JavaScript from now on. In fact, quite the contrary. </p> <p>But one concern that comes to mind that might be interesting for its use is validating the structure of a document.</p> <p>As you might have noticed from my previous posts, we always dealt with one type of object. But a hello-real-world application typically has a domain where multiple types of objects need to be persisted. With an RDBMS, we usually have separate tables to store these different kinds of objects. CouchDB on the other hand only has the notion of storing documents. Now suppose, we need to store both <em>Customer</em> objects and <em>Order</em> objects (every application needs those, right?). How would we deal with that in CouchDB?</p> <p>Well, very simple. We'll just add a <em>Type</em> field to every document.</p><pre class="csharpcode">{
    <span class="str">"Type"</span> : <span class="str">"Customer"</span>
    <span class="str">"FirstName"</span> : <span class="str">"Homer"</span>
    <span class="str">"LastName"</span> : <span class="str">"Simpson"</span>
}

{
    <span class="str">"Type"</span> : <span class="str">"Order"</span>
    <span class="str">"Supplier"</span> : <span class="str">"Duff"</span>
    <span class="str">"Subject"</span> : <span class="str">"Sweet, sweet beer"</span> 
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Just common sense. In order to get a list of all orders, we could provide the following map function:</p><pre class="csharpcode">function(doc) 
{
  <span class="kwrd">if</span>(doc.Type != <span class="str">"Order"</span>) 
    <span class="kwrd">return</span> <span class="kwrd">true</span>

  emit(<span class="kwrd">null</span>, doc);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Piece of cake. Now we can use the validation capabilities of CouchDB to ensure that every document that is stored contains a T<em>ype</em> attribute. They way to handle this is to create a design document that contains an attribute named <em>'validate_doc_update' </em>that specifies a validation function of the following signature:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now in order to keep clear of documents that specify no <em>Type</em> attribute, we could write the following function:</p><pre class="csharpcode"><span class="kwrd">function</span>(newDoc, oldDoc, userCtx)
{
    <span class="kwrd">if</span>(!newDoc.Type)
    {
        <span class="kwrd">throw</span>(
        {
            <span class="str">"Error"</span> : <span class="str">"Documents need a type around here."</span>
        });
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Every time a document is saved or updated, CouchDB will call every validation function that is stored in a design document with the key <em>'validate_doc_update'.</em> When every function passes, the document will be stored. Otherwise, CouchDB will return HTTP status 403 (Forbidden) with a JSON response that contains the error we specified in the validation function.</p>
<p>As you an see, CouchDB provides you with a nice and easy to use validation mechanism that can be useful in a couple of scenarios.</p>
<p>Till next time </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/18/design-documents-in-couchdb-and-validation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Views into CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/views-into-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=views-into-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/views-into-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:41:22 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/views-into-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Adding Attachments to a Document in CouchDB So far, we've mostly talked about managing documents in CouchDB. Now I want [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, here are the links to my previous posts about CouchDB: <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a> <li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a> <li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a> <li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li> <li><a href="http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/">Adding Attachments to a Document in CouchDB</a></li></ol> <p>So far, we've mostly talked about managing documents in CouchDB. Now I want to discuss another important concept of CouchDB, namely views. </p> <p>Views are the primary means for querying and searching documents that are stored by CouchDB. As mentioned in one of my previous posts, CouchDB doesn't support SQL for querying documents. Consequently, views are to CouchDB as SQL is to an RDBMS. They are defined as <a href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> functions using JavaScript. When you've never heard about MapReduce, then take a look at the <a href="http://www.developerzen.com/2009/05/06/introduction-to-mapreduce-for-net-developers/">Introduction to MapReduce for .NET Developers</a> for a quick dip into the concepts behind MapReduce. </p> <p>CouchDB supports two kinds of views, permanent views and temporary views. A permanent view is stored as a special kind of document between the other regular documents. These special kind of documents are called '<em>design documents</em>'. A permanent view can be executed by performing a GET operation with the name of the view.&nbsp; A temporary view, as its name implies, is not stored by CouchDB. Instead, the code for the view is posted to CouchDB where it is executed once. </p> <p>For me, permanent views are the most interesting so we will use this option for the example in this post. Recall that in the examples used in previous posts we've had documents like the following:</p><pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Lets create a view that we can use for retrieving the title of all documents for a particular&nbsp; tag. For creating a permanent view, there are again two options: using <a href="http://couchdb.apache.org/screenshots.html">Futon</a> (the web-based user interface of CouchDB) or through the <a href="http://wiki.apache.org/couchdb/HTTP_view_API">HTTP View API</a>. For keeping things simple, let's use Futon for creating our view in CouchDB.</p>
<p>&nbsp;<a href="http://elegantcode.com/wp-content/uploads/2009/07/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb2.png" width="458" height="324"></a> </p>
<p>When creating a new view, CouchDB provides a map function with a default implementation and an optional reduce function. The purpose of a map function is to perform a number of computations using arbitrary JavaScript and to emit key/value pairs into the view.&nbsp; If the view also has a reduce function, then its used for aggregating the results. We'll ignore the reduce function for now and focus our attention to the map function.&nbsp; </p>
<p>The basic anatomy of the map function as provided by Futon looks like this:</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) {
  emit(<span class="kwrd">null</span>, doc);
}</pre>
<p>As already mentioned, the <em>emit</em> function takes care of inserting a key/value pair of your own choosing into the view. For our example, we'll emit the name of a tag as the key and the title of a document as the value.</p><pre class="csharpcode"><span class="kwrd">function</span>(doc) 
{
  <span class="kwrd">for</span> each(<span class="kwrd">var</span> tag <span class="kwrd">in</span> doc.Tags)
  {
    emit(tag.Name, doc.Title);
  }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p><a href="http://elegantcode.com/wp-content/uploads/2009/07/image3.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/07/image-thumb3.png" width="465" height="331"></a> </p>
<p>Using Futon it's possible to code up a map and reduce function and try it out on the documents that are stored in CouchDB. Executing the map function as is using an HTTP GET operation yields the following results:</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag HTTP/1.1

{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>
},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Physics"</span>,<span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>},
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Space"</span>,<span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We call the view by using its name in the URL. The result is a key for every tag we encounter and the title of the document as value. CouchDB also provides the document identifier for each key/value pair. This way we can see that we have five tags for two documents. </p>
<p>Now in order to get all distinct document titles for a particular tag, we have to add the tag name as an extra query parameter called '<em>key</em>':</p><pre class="csharpcode">GET /documentstore/_design/documents_by_tag/
_view/documents_by_tag?key=%22Universe%22 HTTP/1.1</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This yields the following result from our view:</p><pre class="csharpcode">{<span class="str">"total_rows"</span>:5,<span class="str">"offset"</span>:0,<span class="str">"rows"</span>:[
{
 <span class="str">"id"</span>:<span class="str">"0afc1fc2-7b39-461f-87cf-1ed1e21d2f34"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Universe in a Nutshell"</span>},
{
 <span class="str">"id"</span>:<span class="str">"7b287e5d-c467-46ad-a10e-66d3c0696743"</span>,
 <span class="str">"key"</span>:<span class="str">"Universe"</span>,
 <span class="str">"value"</span>:<span class="str">"The Theory of Everything"</span>}
]}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This way we can also use different tag names and reuse our view. Notice that the total_rows attribute indicates that the original result set from our view contains five objects.</p>
<p>Doesn't seem to be very hard now doesn't it? Please do mind that I've barely scratched the surface here. For more information, you can take a look at the <a href="http://wiki.apache.org/couchdb/">CouchDB wiki</a> or check out these forthcoming books:</p>
<ul>
<li><a href="http://www.manning.com/chandler/">CouchDB in Action</a>
<li><a href="http://books.couchdb.org/relax/">CouchDB: The Definitive Guide</a></li></ul>
<p>If you're interested in distributed, non-relational databases in general you might want to check out the <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">recordings from the NOSQL meetup</a> that also hosted a talk on CouchDB as well. </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/views-into-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Attachments to a Document in CouchDB</title>
		<link>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-attachments-to-documents-in-couchdb</link>
		<comments>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 13:45:39 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB DELETE Documents From CouchDB Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/">GETting Documents From CouchDB</a></li>
	<li><a href="http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/">DELETE Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to create attachments for a document. Documents in CouchDB can have attachments just like an email. CouchDB has two ways for dealing with attachments:
<ol>
	<li>Inline Attachments</li>
	<li>Standalone Attachments</li>
</ol>
<h3>Inline Attachments</h3>
Inline attachments can be added to a document by using the dedicated <em>_attachments</em> attribute while <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting</a> the document into CouchDB.
<pre class="csharpcode">PUT /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
  <span class="str">"The Universe in a Nutshell.pdf"</span>:
  {
    <span class="str">"content_type"</span>: <span class="str">"application/pdf"</span>,
    <span class="str">"data"</span>: <span class="str">"JVBERi0xLjUNCiW1tbW1DQox ... "</span>
  }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

For creating an attachment, we need to provide a file name, the MIME type and the base64 encoded binary data. Its even possible to have multiple attachments for a single document.
<h3>Standalone Attachments</h3>
Standalone attachments are a fairly recent feature of CouchDB that has been added to version 0.9. As it name implies, it involves adding, updating and removing attachments without the document itself being involved.
<pre class="csharpcode">PUT documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf?rev=1-1437623276 HTTP/1.0
Content-Length: 911240
Content-Type: application/pdf

JVBERi0xLjUNCiW1tbW1DQox ...</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

The major difference and advantage of this approach is that the binary data is sent directly to CouchDB without the need for a base64 conversion on both the client and server. This implies a significant performance improvement when storing attachments in CouchDB. Notice that you still need to provide a MIME type using the Content-Type header.
<h3>GETting Documents with Attachments</h3>
When retrieving a document with either an inline or standalone attachment, the actual binary data is not returned. Instead, CouchDB returns a stub to inform that the requested document has an attachment associated with it.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325 HTTP/1.1

{
<span class="str">"_id"</span>:<span class="str">"4f754d4b-540d-4b77-8507-6b7243ef8325"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-1969924333"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}],
<span class="str">"_attachments"</span>:
{
    <span class="str">"The Universe in a Nutshell.pdf"</span>:
    {
        <span class="str">"stub"</span>:<span class="kwrd">true</span>,
        <span class="str">"content_type"</span>:<span class="str">"application/pdf"</span>,
        <span class="str">"length"</span>:911240}
    }
}}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the binary data of the attachment itself (yes please!), we have to issue a second GET but now using both the document identifier and the file name of the attachment.
<pre class="csharpcode">GET /documentstore/4f754d4b-540d-4b77-8507-6b7243ef8325/
The%20Universe%20in%20a%20Nutshell.pdf HTTP/1.1</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB responds to this request by returning the binary data of the attachment. When an inline attachment is used, the binary data automatically gets decoded.

For my next post, I will talk about MapReduce functions providing a simple example of a Map function in particular.

Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/10/adding-attachments-to-documents-in-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DELETE Documents from CouchDB</title>
		<link>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:21:55 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room GETting Documents From CouchDB Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/">PUTting the Couch(DB) in Your Living Room</a></li>
	<li><a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/" target="_blank">GETting Documents From CouchDB</a></li>
</ol>
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
<pre class="csharpcode">DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=1-2901013762</pre>
This makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3500205450"</span>
}</pre>
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:

<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<pre class="csharpcode">GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb
?rev=2-3500205450</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

still indicates that the document we just deleted still exists:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"13ce4780-62c8-4074-9955-8c99966b84bb"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-3500205450"</span>,
<span class="str">"_deleted"</span>:<span class="kwrd">true</span>
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Notice that the <em>_deleted</em> attribute indicates that the document has indeed been deleted. It's even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.

However, I don't consider that a good idea in all scenarios. In fact, as pointed out by <a href="http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comment-47440" target="_blank">this comment</a> on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. <strong>When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended.</strong> In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn't necessarily have the complete version history of a document. Bottom line, use this feature very carefully.

For my next post, I will talk about how to create attachment for a document.

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/03/delete-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETting Documents From CouchDB</title>
		<link>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-documents-from-couchdb</link>
		<comments>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:22:23 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/</guid>
		<description><![CDATA[To start off, here are the links to my previous posts about CouchDB: Relaxing on the Couch(DB) Installing the Couch(DB) PUTting the Couch(DB) in Your Living Room In my latest post, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document [...]]]></description>
			<content:encoded><![CDATA[To start off, here are the links to my previous posts about CouchDB:
<ol>
	<li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/">Relaxing on the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/">Installing the Couch(DB)</a></li>
	<li><a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">PUTting the Couch(DB) in Your Living Room</a></li>
</ol>
In my <a href="http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/" target="_blank">latest post</a>, I explained how easy it is to create and manage documents in CouchDB. Today, I want to talk about how to retrieve a document from CouchDB.

In order to retrieve a document from CouchDB, we make use of the HTTP GET operation (duh). Using the GET method for retrieving a document takes the following general form:

<a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a>

Its as simple as that. So, for example, sending the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

makes CouchDB return the following response:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As already mentioned, CouchDB provides MVCC (multi-version concurrency control) for the documents it stores. Using the GET operation makes it possible to take advantage of this built-in feature. Notice that the first digit of the revision number in the example above indicates that this is the second version of the document we retrieved. This means that CouchDB possibly has an earlier version of the document. In order to find out what revisions are available, we can issue the following request:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?revs=<span class="kwrd">true</span></pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

CouchDB returns the current revision of the document as before, but now with an additional field named <em>_revisions</em>:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"2-1534297415"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str">"Name"</span>:<span class="str">"Space"</span>}],
<span class="str">"_revisions"</span>:{<span class="str">"start"</span>:2,<span class="str">"ids"</span>:[<span class="str">"1534297415"</span>,<span class="str">"3158114761"</span>]},
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

In order to retrieve the first revision of our document, we can send the following request to retrieve it:
<pre class="csharpcode">GET /documentstore/96f49e5a-6b5b-47ed-9234-9a98d600013e?rev=1-3158114761</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

As expected, CouchDB now returns the first revision of the document:
<pre class="csharpcode">{
<span class="str">"_id"</span>:<span class="str">"96f49e5a-6b5b-47ed-9234-9a98d600013e"</span>,
<span class="str">"_rev"</span>:<span class="str">"1-3158114761"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

It seems that for the second revision of the document, we have added an extra tag. Having this kind of automatic versioning for your data is a really nice feature in my book.

For my next post, I will talk about how to delete a document from CouchDB..

Till next time]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/02/getting-documents-from-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting the Couch(DB) in Your Living Room</title>
		<link>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=putting-the-couchdb-in-your-living-room</link>
		<comments>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:04:29 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/</guid>
		<description><![CDATA[In my previous posts, I provided a shallow introduction to CouchDB and how to get it installed on a Linux box. Here are the links to these posts: Relaxing on the Couch(DB) Installing the Couch(DB) Now, for this post I want to talk about how to create and manage documents. As I already mentioned in [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous posts, I provided a shallow introduction to <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> and how to get it installed on a Linux box. Here are the links to these posts:</p> <ol> <li><a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">Relaxing on the Couch(DB)</a>  <li><a href="http://elegantcode.com/2009/05/30/installing-the-couchdb/" target="_blank">Installing the Couch(DB)</a></li></ol> <p>Now, for this post I want to talk about how to create and manage documents. As I already mentioned in my introductory blog post, CouchDB is accessible through a RESTful HTTP/JSON API. This means that documents are stored as JSON objects. Let me show you an example of how to store a document in CouchDB.&nbsp; </p> <p>Suppose we have a class named <em>Document</em> (how creative of me). A <em>Document </em>has a title, an author and a collection of <em>Tag</em> objects. A <em>Tag</em> is a value object that has a name.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Document
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> ISet&lt;Tag&gt; _tags;

    <span class="kwrd">public</span> String Author { get; }
    <span class="kwrd">public</span> String Title { get; }
    <span class="kwrd">public</span> Guid Id { get; }
    <span class="kwrd">public</span> String Version { get; }

    <span class="kwrd">public</span> IEnumerable&lt;Tag&gt; Tags
    {
        get { <span class="kwrd">return</span> _tags; }
    }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Tag : ValueObject&lt;Tag&gt;
{
    <span class="kwrd">public</span> String Name { get; <span class="kwrd">private</span> set; }

    <span class="kwrd">public</span> Tag(String name)
    {    
        Name = name;
        RegisterProperty(<span class="kwrd">value</span> =&gt; <span class="kwrd">value</span>.Name);
    }
} </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Nothing fancy so far. Now in order to save an instance of this class, we have to serialize it to its JSON representation. I've been using <a href="http://www.codeplex.com/Json" target="_blank">Json.NET</a> for this purpose, but you might as well use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" target="_blank">JavaScriptSerializer</a> class from the .NET framework.</p>
<p>Now, in order to save a document, we make use of the PUT HTTP method. This implies that we have to provide our own document identifier. We have to initialize the <em>Id</em> property with a new GUID for a new <em>Document</em>. We could also make use the POST HTTP method. This way you don't have to specify your own document identifier and let CouchDB generate one for you when the document is initially stored. But this is discouraged because proxies and other network intermediaries are able to resend POST requests resulting in duplicate documents.</p>
<p>Creating a new document in CouchDB using the PUT method takes the following general form:</p>
<blockquote>
<p><a href="http://myhost:5984/my_database/my_id">http://myhost:5984/my_database/my_id</a></p></blockquote>
<p>So for our example, sending the following request:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1215256
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>makes CouchDB return the following response:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"1-1437623276"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>You probably figured out by now that this response means that the document has successfully been stored by CouchDB. Notice that it also returns a revision number that was generated upon creating the document in the database. This is important when we try to save changes to the document later on. </p>
<p>If you're as suspicious as I am, you probably want to verify whether CouchDB&nbsp; correctly stored the document we've provided. You can do this using <a href="http://couchdb.apache.org/screenshots.html" target="_blank">Futon</a>, the web-based user interface of CouchDB. You can access Futon through the following URL:</p>
<blockquote>
<p><a href="http://myhost:5984/_utils">http://myhost:5984/_utils</a></p></blockquote>
<p>Say that we already want to make some changes to the document we just created. Lets add a new tag to the document named <em>'Space'</em>. We add a new <em>Tag</em> object to the instance of our <em>Document, </em>we serialize it back to its JSON representation and send the following request, asking CouchDB to save the changes:</p><pre class="csharpcode">PUT /documentstore/80c8675d-2015-45f5-a3c7-098226e15ce3 HTTP/1.1
Content-Type: application/json
Host: 146.135.16.100:5984
Content-Length: 1705803
Expect: 100-<span class="kwrd">continue</span>

{
<span class="str">"_rev"</span>:<span class="str">"1-1437623276"</span>,
<span class="str">"Author"</span>:<span class="str">"Stephen Hawking"</span>,
<span class="str">"Title"</span>:<span class="str">"The Universe in a Nutshell"</span>,
<span class="str">"Tags"</span>:[{<span class="str">"Name"</span>:<span class="str">"Physics"</span>},{<span class="str">"Name"</span>:<span class="str">"Universe"</span>},{<span class="str"><font color="#ff0000">"Name"</font></span>:<span class="str"><font color="#ff0000">"Space"</font></span>}]
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Notice that we are using the PUT method again, but now we also provided the revision number we got back from our first response when we initially created the document. Also notice that we are sending the complete document again, but now with the extra tag. </p>
<p>CouchDB returns the following response that contains a new revision number for the document:</p><pre class="csharpcode">{
<span class="str">"ok"</span>:<span class="kwrd">true</span>,
<span class="str">"id"</span>:<span class="str">"80c8675d-2015-45f5-a3c7-098226e15ce3"</span>,
<span class="str">"rev"</span>:<span class="str">"2-3213552600"</span>
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Again, you can use Futon to verify if CouchDB stored a new version of the document. For my next post on CouchDB, I will talk about how retrieve a document from CouchDB.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/01/putting-the-couchdb-in-your-living-room/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Installing the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/30/installing-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/30/installing-the-couchdb/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:59:02 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/30/installing-the-couchdb/</guid>
		<description><![CDATA[In my previous post, I talked about some introductory topics regarding CouchDB. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for [...]]]></description>
			<content:encoded><![CDATA[In my <a href="http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/" target="_blank">previous post</a>, I talked about some introductory topics regarding <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>. In this post I want to walk you through some of the hurdles you need to take when you want to install CouchDB on a freshly installed Ubuntu Linux 9.0.4. Now I don't want to claim that there isn't a simpler way for getting CouchDB up and running, which probably should be the case. I just want to put out everything I had to deal with when I tried installing the trunk version (V9.0) of CouchDB. I didn't install the pre-packaged version (V8.02) as that would be too easy :-). Here goes ...

1. Prepare your environment:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essentialerlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget</span>

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install libcurl4-gnutls-dev</span>

2. Create a new user for running CouchDB:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo adduser --no-create-home --disabled-password --disabled-login couchdb</span>

3. Get the latest source code from the trunk:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">svn co </span><a href="http://svn.apache.org/repos/asf/couchdb/trunk"><span style="font-family: Courier New; color: #008000; font-size: x-small;">http://svn.apache.org/repos/asf/couchdb/trunk</span></a><span style="font-family: Courier New; color: #008000; font-size: x-small;"> couchdb</span>

4. Build and install: 

<span style="font-family: Courier New; color: #008000; font-size: x-small;">./bootstrap
./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc
make &amp;&amp; sudo make install</span>

5. Make the CouchDB user, that was created in step 2, the owner of the installed binaries:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo chown couchdb:couchdb -R  /var/lib/couchdb /var/log/couchdb</span>

6. Now we're all set to launch the CouchDB server:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo -i -u couchdb couchdb</span>

If everything goes well, you should see something similar as the following output:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo: unable to change directory to /home/couchdb: No such file
or directory
Apache CouchDB 0.10.0a780291 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.1.0&gt;] Apache CouchDB has started.</span>

7. In order to verify that CouchDB is up and running, you can install <a href="http://en.wikipedia.org/wiki/CURL" target="_blank">cURL</a> and issue the following command:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo apt-get install curl
curl http://localhost:5984</span>

This should give you the following response:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">{"couchdb":"Welcome","version":"0.10.0a780291"}</span>

8. [Optional] Register  CouchDB as a service:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo update-rc.d couchdb defaults
sudo /etc/init.d/couchdb start</span>

 

So far in our setup, we can only access CouchDB from the local Linux (virtual) machine. My personal goal was to access CouchDB from a sample application that I'm developing on another Windows virtual machine that hosts my development environment. In order to accomplish that, some additional steps need to be taken:

1. Open the correct port on the firewall when enabled. You can do this by issuing the following:

<span style="font-family: Courier New; color: #008000; font-size: x-small;">sudo iptables -I INPUT 3 -p tcp --dport 5984 -j ACCEPT</span>

2. Go to the <em>local.ini</em> file in the <em>/etc/couchdb/</em> directory and uncomment the line that specifies the <em>bind_address</em> setting (in the <em>httpd</em> section) by removing the semicolon in front of it (figuring out this one kept me busy for quite some time ;-) ).

3. Change the <em>bind_address</em> setting so that it now specifies 0.0.0.0 instead of 127.0.0.0. After restarting CouchDB, it will now bind to all addresses.

4. We're done.

As I mentioned before, I'm quite a Linux rookie so it was definitely a challenge figuring this out.

If this all seems too much for you, then you might be interested to take a look at <a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html" target="_blank">Interactive CouchDB</a>. This is an online CouchDB emulator/visualizer that was completely written in JavaScript. It provides a sample DB where can play around with some map/reduce functions which I'm going to discuss in one of my next posts.

Now I need some relaxation. Till next time.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/30/installing-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relaxing on the Couch(DB)</title>
		<link>http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=relaxing-on-the-couchdb</link>
		<comments>http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/#comments</comments>
		<pubDate>Sun, 24 May 2009 00:43:52 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/</guid>
		<description><![CDATA[The last couple of months, I heard some buzz around CouchDB at several user groups. Listening to this podcast really got me interested, so I decided to learn more about it in order to find out what all the fuzz is about. Is CouchDB yet another relational database? Heck no! CouchDB is an open-source document-oriented [...]]]></description>
			<content:encoded><![CDATA[The last couple of months, I heard some buzz around <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a> at several user groups. Listening to <a href="http://odeo.com/episodes/24385200-Introduction-to-CouchDB-by-CouchDB-Podcast" target="_blank">this podcast</a> really got me interested, so I decided to learn more about it in order to find out what all the fuzz is about.

<a href="http://elegantcode.com/wp-content/uploads/2009/05/couchdb-logo.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" src="http://elegantcode.com/wp-content/uploads/2009/05/couchdb-logo-thumb.png" border="0" alt="couchdb-logo" width="244" height="70" /></a>

Is CouchDB yet another relational database? Heck no! CouchDB is an open-source document-oriented database. Its originally created by <a href="http://damienkatz.net/" target="_blank">Damien Katz</a> and developed using <a href="http://erlang.org/" target="_blank">Erlang</a>. The data stored in CouchDB cannot be accessed using traditional SQL but through a RESTful API. CouchDB can be installed on most <a href="http://en.wikipedia.org/wiki/POSIX" target="_blank">POSIX</a> systems, including Linux and Mac OS. It can also be installed on Windows, but this isn't officially supported and according to <a href="http://wiki.apache.org/couchdb/Installing_on_Windows" target="_blank">this page</a> on the wiki, not all IO-related features are fully functional. But don't let that scare you away. It can be fun to spend some time with <a href="http://www.ubuntu.com/" target="_blank">Ubuntu</a> :-). The final goal of CouchDB is to provide a scalable, reliable and fault-tolerant document database that can run on failure prone systems.

So, what's the difference between a relational database and a document-oriented database and why would you care? As you probably know, the center of the RDBMS world is the <em>row</em>. It may not come to a surprise that the centerpiece of a document-oriented database is the <em>document</em>.

A document in a document-oriented database is completely self-contained. There are no tables, rows, foreign keys, joins and more importantly, there is <strong>no database schema at all</strong>! You can store whatever data you'd like for a particular document and use a completely different set of data for another. Once you have a set of documents stored, you can add/remove data of one or more documents without affecting others.

For example, when your application wants to store customer information like the first and last name, then these attributes can be stored in a document for every customer. As documents in CouchDB are stored as JSON, a customer document would look like this:
<div id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ced7c4e4-b34a-4480-8aa8-749a66f52da8" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000; ">{
    </span><span style="color: #000000; ">"</span><span style="color: #000000; ">_id</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">...</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">_rev</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">...</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">FirstName</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">Chuck</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">LastName</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">Norris</span><span style="color: #000000; ">"</span><span style="color: #000000; ">
}</span></div></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
Suppose you application has been in production for almost a year now and by celebrating this fact, the shoe size of the customer must be stored as well.

Simple. You don't have to change any schema or anything (and face those nasty DBA's). You just start saving the valuable shoe size information for every new/existing customer from now on.
<div id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:71cae7cd-dc5a-4547-8c69-1f0ff3021283" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000; ">{
    </span><span style="color: #000000; ">"</span><span style="color: #000000; ">_id</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">...</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">_rev</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">...</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">FirstName</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">Chuck</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">LastName</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">Norris</span><span style="color: #000000; ">"</span><span style="color: #000000; ">
   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">ShoeSize</span><span style="color: #000000; ">"</span><span style="color: #000000; "> : </span><span style="color: #000000; ">"</span><span style="color: #000000; ">52</span><span style="color: #000000; ">"</span><span style="color: #000000; ">
}</span></div></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
You probably want to update the already existing documents as soon as you can get a hold of this extra information, but the point is that you don't have to.

Another major difference between a relational database and a document-oriented database are the unique identifiers. A table in a RDBMS typically has a primary key in order to identify a row. The value of the primary key column is mostly generated by some sequence generator. This means that the identifier of a row is only unique for the table itself. CouchDB on the other hand, provides a document identifier that must be unique across multiple databases. It has to be unique on a global scale because in order to achieve high availability, CouchDB has a replication feature built in so that it's possible to replicate with multiple database instances across multiple machines. The safest way for uniquely identifying a document is to provide a GUID or let CouchDB generate a unique randomly generated identifier for you.

Another interesting feature of CouchDB is the way it handles versioning. But first lets talk a bit how you would traditionally solve this using a relational database.

In order to achieve optimistic-concurrency in a RDBMS, you have to include a version column that you have to update with every row change and use it in the WHERE clause of the update statement involved. But what if you want to know what got changed to a particular row last month? In order to store that kind of information, you have to create a separate table where you insert a snapshot of the row for every update statement that gets executed on the table (probably using a trigger). The point that I'm trying to make here is that a relational database is not going to help you with that. You have to provide all this plumbing yourself.

As you may have noticed from the customer example above, there are two extra fields <em>_id</em> and <em>_rev</em>. Every document in CouchDB contains this extra meta-data. We just discussed that the document identifier is stored in the <em>_id</em> field. The revision of the document is stored in the <em>_rev</em> field. When you create a new document, this revision field will be filled by CouchDB and returned to you. Every change from that moment on will not be made to the current existing document, but a new version of the entire document  is created. This means that the complete history of a document is automatically captured and maintained by the database. The revision number is also used for implementing concurrency. When two clients want to make changes to the same document, the first client will be able to store his changes while the second one will receive a notification about the conflict.

That's all fine and sweet. But what's the point of having a document database filled with all the information I want, but not having SQL at my disposal to get it back out?

CouchDB relies on views to retrieve all the information you want out of your documents. These views are computed using <a href="http://en.wikipedia.org/wiki/MapReduce" target="_blank">Map/Reduce</a>, a way of dealing with large sets of data using parallelism. This concept originally comes from the <a href="http://en.wikipedia.org/wiki/Lisp_programming_language" target="_blank">Lisp</a> programming language, but is popularized by Google. You can read the paper <a href="http://labs.google.com/papers/mapreduce.html" target="_blank">here</a>.

I'm planning to write a couple of blog posts in the near future about the things I've learned so far and the new things I'm going to learn about CouchDB along the way. In the meanwhile, if you can't wait to learn more, then check out these forthcoming books:
<ul>
	<li> <a href="http://www.manning.com/chandler/" target="_blank">CouchDB in Action</a></li>
	<li> <a href="http://books.couchdb.org/relax/" target="_blank">CouchDB: The Definitive Guide</a></li>
</ul>
I've only read the MEAP of CouchDB in Action which is only about 30 pages so far, but looks very promising.

For my next post on CouchDB, I will try to make a summary of the steps I have gone through for installing CouchDB on a clean Ubuntu Linux installation.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/23/relaxing-on-the-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

