<?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>Mark Needham &#187; CouchDB</title>
	<atom:link href="http://www.markhneedham.com/blog/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +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>CouchDB: Join like behaviour with link functions</title>
		<link>http://www.markhneedham.com/blog/2011/02/13/couchdb-join-like-behaviour-with-link-functions/</link>
		<comments>http://www.markhneedham.com/blog/2011/02/13/couchdb-join-like-behaviour-with-link-functions/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 17:58:54 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3348</guid>
		<description><![CDATA[I&#8217;ve been playing around with the Twitter streaming API a bit lately to see which links are being posted most frequently by the people I follow and then storing the appropriate tweets in CouchDB. I recently came across a problem which I struggled to solve for quite a while. Based on the following map function: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with the <a href="http://apiwiki.twitter.com/w/page/22554673/Streaming-API-Documentation">Twitter streaming API</a> a bit lately to see which links are being posted most frequently by the people I follow and then storing the appropriate tweets in CouchDB.</p>
<p>I recently came across a problem which I struggled to solve for quite a while.</p>
<p>Based on the following map function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;_design/query&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;views&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;by_link&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;map&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;function(doc){ emit(doc.actual_link, { user : doc.user.screen_name, text : doc.text })}&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which results in the following data set:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">curl http://127.0.0.1:5984/twitter_links/_design/query/_view/by_link?limit=20</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{&quot;total_rows&quot;:7035,&quot;offset&quot;:0,&quot;rows&quot;:[
{&quot;id&quot;:&quot;abf54db1d92bfe0e8aaaa9ec51f237bd&quot;,&quot;key&quot;:&quot;http://2dboy.com/2011/02/08/ipad-launch/&quot;,&quot;value&quot;:{&quot;user&quot;:&quot;Nash&quot;,&quot;text&quot;:&quot;World of Goo\u2019s iPad Launch http://instapaper.com/zzqrqw32e&quot;}},
{&quot;id&quot;:&quot;b8911545ff45438671081260ae0d42b1&quot;,&quot;key&quot;:&quot;http://3.bp.blogspot.com/_T6MpHfZv2qQ/SpKGGjsoQoI/AAAAAAAADIA/Jsa5JDqX9X0/s400/moleskine3.jpg&quot;,&quot;value&quot;:{&quot;user&quot;:&quot;oinonio&quot;,&quot;text&quot;:&quot;@stephenfry a Babushka Little My? http://bit.ly/fjPg2a&quot;}},
{&quot;id&quot;:&quot;be12d30d1c8b882d8ce0124585fabb19&quot;,&quot;key&quot;:&quot;http://3.bp.blogspot.com/_UAzEooLfuI8/S7aOiCBdAzI/AAAAAAAAF8Y/5W61I9VHxPE/s1600-h/deforestation.jpg&quot;,&quot;value&quot;:{&quot;user&quot;:&quot;ironshay&quot;,&quot;text&quot;:&quot;A big problem caused by deforestation http://bit.ly/9qArCg&quot;}}
]}</pre></div></div>

<p>What I want to do is go from&#8230;</p>
<ul>
<li>Link Url 1 -> Tweet 1</li>
<li>Link Url 1 -> Tweet 2</li>
<li>Link Url 2 -> Tweet 3</li>
</ul>
<p>&#8230;to&#8230;</p>
<ul>
<li>Link Url 1 -> [Tweet 1, Tweet 2]</li>
<li>Link Url 2 -> [Tweet3]</li>
</ul>
<p>I originally tried to use a reduce function after following <a href="http://chrischandler.name/couchdb/view-collation-for-join-like-behavior-in-couchdb/">Chris Chandler&#8217;s blog post</a> but that resulted in a &#8216;reduce_overflow_error&#8217;.</p>
<p><a href="http://twitter.com/perrynfowler/status/36372745549774848">Perryn</a> pointed out that what I probably needed was a link function and I came across <a href="http://japhr.blogspot.com/2010/02/collating-not-reducing-with-couchdb.html">Chris Strom&#8217;s blog</a> while trying to work out how to do that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;_design/query&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;views&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;by_link&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;map&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;function(doc){ emit(doc.actual_link, { user : doc.user.screen_name, text : doc.text })}&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;lists&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;index_tweets&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;function(head, req) {
     var row, last_key, tweets;
     send('{<span style="color: #000099; font-weight: bold;">\&quot;</span>rows<span style="color: #000099; font-weight: bold;">\&quot;</span> : [');
     while(row = getRow()) {
      if(last_key != row.key ) {
        if(last_key != 'undefined') {
          send(toJSON({key : last_key, values : tweets}));
          send(',');
        }
        tweets = [];
        last_key = row.key;
      } 
      tweets.push(row.value);
     }
     send(toJSON({key : last_key, values : tweets}));
     send(']}');
  }&quot;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We then call the list function with an associated view function following this pattern from <a href="http://dev.twitter.com/pages/streaming_api">CouchDB: The Definitive Guide</a>:</p>
<blockquote><p>
/db/_design/foo/_list/list-name/view-name
</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">curl http://127.0.0.1:5984/twitter_links/_design/query/_list/index_tweets/by_link</pre></div></div>

<p>Which gives the data in the required format:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{&quot;rows&quot; : [
{&quot;key&quot;:&quot;http://1.bp.blogspot.com/_XdP6Lp2ceqY/TU16NvdT-RI/AAAAAAAAlb8/7QtTN-XxBTM/s400/dcrHk.jpg&quot;,&quot;values&quot;:[{&quot;user&quot;:&quot;jhartikainen&quot;,&quot;text&quot;:&quot;RT @codepo8: The dark secret of PacMan: http://bit.ly/exCBDy&quot;}, {&quot;user&quot;:&quot;joedevon&quot;,&quot;text&quot;:&quot;RT @codepo8: The dark secret of PacMan: http://bit.ly/exCBDy&quot;}]},
{&quot;key&quot;:&quot;http://10poundpom.blogspot.com/&quot;,&quot;values&quot;:[{&quot;user&quot;:&quot;10poundpomCL&quot;,&quot;text&quot;:&quot;@andy_murray Help my #£10aWeekCharityChallenge, all it takes is a RT. Read http://10poundpom.blogspot.com/ for more.&quot;}]},
{&quot;key&quot;:&quot;http://10rem.net/blog/2011/02/09/enhancing-the-wpf-screen-capture-program-with-window-borders&quot;,&quot;values&quot;:[{&quot;user&quot;:&quot;brian_henderson&quot;,&quot;text&quot;:&quot;Enhancing the WPF Screen Capture Program with Window Borders: by @Pete_Brown: http://bit.ly/icmXG5 #wpf #win32&quot;},{&quot;user&quot;:&quot;SittenSpynne&quot;,&quot;text&quot;:&quot;RT @Pete_Brown: Blogged: Enhancing the WPF Screen Capture Program with Window Borders http://bit.ly/icmXG5 #wpf #win32&quot;}]}]}</pre></div></div>

<p>Maybe there&#8217;s an even better way to solve this problem that I don&#8217;t know about&#8230;let me know if there is!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/02/13/couchdb-join-like-behaviour-with-link-functions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CouchDB: &#8216;badmatch&#8217; when executing view</title>
		<link>http://www.markhneedham.com/blog/2011/02/12/couchdb-badmatch-when-executing-view/</link>
		<comments>http://www.markhneedham.com/blog/2011/02/12/couchdb-badmatch-when-executing-view/#comments</comments>
		<pubDate>Sat, 12 Feb 2011 18:03:53 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[CouchDB]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3345</guid>
		<description><![CDATA[I&#8217;ve been playing around with CouchDB again in my annual attempt to capture the links appearing on my twitter stream and I managed to create the following error for myself: $ curl http://127.0.0.1:5984/twitter_links/_design/cleanup/_view/find_broken_links {&#34;error&#34;:&#34;badmatch&#34;,&#34;reason&#34;:&#34;{\n \&#34;find_broken_links\&#34;: {\n \&#34;map\&#34;: \&#34;function(doc) { \nvar prefix = doc.actual_link.match(/.*/); \n if(true) { emit(doc.actual_link, null); } }\&#34;\n }\n}&#34;} It turns out this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with <a href="http://couchdb.apache.org/">CouchDB</a> again in my annual attempt to capture the links appearing on my twitter stream and I managed to create the following error for myself:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ curl http://127.0.0.1:5984/twitter_links/_design/cleanup/_view/find_broken_links
{&quot;error&quot;:&quot;badmatch&quot;,&quot;reason&quot;:&quot;{\n   \&quot;find_broken_links\&quot;: {\n       \&quot;map\&quot;: \&quot;function(doc) {   \nvar prefix = doc.actual_link.match(/.*/);            \n  if(true) {                  emit(doc.actual_link, null);                }              }\&quot;\n   }\n}&quot;}</pre></div></div>

<p>It turns out this error is because I&#8217;ve managed to create new line characters in the view while editing it inside <a href="http://janl.github.com/couchdbx/">CouchDBX</a>. D&#8217;oh!</p>
<p>A better way is to edit the view in a text editor and then send it to CouchDB using curl.</p>
<p>The <a href="http://wiki.apache.org/couchdb/HTTP_Document_API?action=show&#038;redirect=HttpDocumentApi#PUT">proper way</a> to update a view would be to add a &#8216;_rev&#8217; property to the body of the JSON document but I find it annoying to go and edit the document so I&#8217;ve just been deleting and then recreating my views.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ curl -X GET http://127.0.0.1:5984/twitter_links/_design/cleanup/
{&quot;_id&quot;:&quot;_design/cleanup&quot;,&quot;_rev&quot;:&quot;1-8be14d29f183b61f1ade160badef3f75&quot;,&quot;views&quot;...}</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ curl -X DELETE http://127.0.0.1:5984/twitter_links/_design/cleanup?rev=1-8be14d29f183b61f1ade160badef3f75
{&quot;ok&quot;:true,&quot;id&quot;:&quot;_design/cleanup&quot;,&quot;rev&quot;:&quot;2-9fa15c1fdbb7cbaa659d623bc897b9da&quot;}</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ curl -X PUT http://127.0.0.1:5984/twitter_links/_design/cleanup -d @cleanup.json
{&quot;ok&quot;:true,&quot;id&quot;:&quot;_design/cleanup&quot;,&quot;rev&quot;:&quot;17-b0763381b79f3fda843f57a7dcc842e1&quot;}</pre></div></div>

<p>I guess there&#8217;s probably a library somewhere which would encapsulate all that for me but I&#8217;m just hacking around at the moment. </p>
<p>It&#8217;s interesting to to see how you interact differently with a document database compared to what you&#8217;d do with a relational one with respect to optimistic concurrency.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/02/12/couchdb-badmatch-when-executing-view/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CouchDB/Futon: &#8216;_all_dbs&#8217; call returns databases with leading &#8216;c/&#8217;</title>
		<link>http://www.markhneedham.com/blog/2009/05/31/couchdbfuton-_all_dbs-call-returns-databases-with-leading-c/</link>
		<comments>http://www.markhneedham.com/blog/2009/05/31/couchdbfuton-_all_dbs-call-returns-databases-with-leading-c/#comments</comments>
		<pubDate>Sun, 31 May 2009 13:28:20 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[futon]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1278</guid>
		<description><![CDATA[As I mentioned in my previous post I&#8217;ve been playing around with CouchDB and one of the problems that I&#8217;ve been having is that although I can access my database through the REST API perfectly fine, whenever I went to the Futon page (&#8216;http://localhost:5984/_utils/&#8217; in my case) to view my list of databases I was [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned <a href="http://www.markhneedham.com/blog/2009/05/31/sharpcouch-use-anonymous-type-to-create-json-objects/">in my previous post</a> I&#8217;ve been playing around with <a href="http://wiki.apache.org/couchdb/FrontPage">CouchDB</a> and one of the problems that I&#8217;ve been having is that although I can access my database through the REST API perfectly fine, whenever I went to the <a href="https://forge.process-one.net/browse/~raw,r=811/CouchDB/trunk/share/www/index.html">Futon</a> page (&#8216;http://localhost:5984/_utils/&#8217; in my case) to view my list of databases I was getting the following javascript error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Database information could not be retrieved: missing</pre></div></div>

<p>I thought I&#8217;d have a quick look with FireBug to see if I could work out what was going on and saw several requests being made to the following urls and resulting in 404s:</p>
<ul>
<li>http://localhost:5984/c%2Fsharpcouch/</li>
<li>http://localhost:5984/c%2Fmark_erlang/</li>
</ul>
<p>The value &#8216;c/&#8217; was being added to the front of each of my database names, therefore meaning that Futon was unable to display the various attributes on the page for each of them.</p>
<p>Tracing this further I realised that the call to &#8216;http://localhost:5984/_all_dbs&#8217; was actually the one that was failing, and calling it directly from &#8216;erl&#8217; was resulting in the same error:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">&gt;</span> couch_server:<span style="color: #ff3c00;">all_databases</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #ff7800;">&quot;c/mark_erlang&quot;</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;c/sharpcouch&quot;</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;c/test_suite_db&quot;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#125;</span></pre></div></div>

<p>I don&#8217;t know Erlang well enough to try and change the code to fix this problem but I came across a <a href="https://issues.apache.org/jira/browse/COUCHDB-307">bug report</a> on the CouchDB website which described exactly the problem I&#8217;ve been having.</p>
<p>Apparently there is a problem when you use an upper case &#8216;C&#8217; for the &#8216;DbRootDir&#8217; property in &#8216;couch.ini&#8217;. Changing that to a lower case &#8216;c&#8217; so that my &#8216;couch.ini&#8217; file now looks like this solved the problem:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DbRootDir=c:/couchdb/db</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/05/31/couchdbfuton-_all_dbs-call-returns-databases-with-leading-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SharpCouch: Use anonymous type to create JSON objects</title>
		<link>http://www.markhneedham.com/blog/2009/05/31/sharpcouch-use-anonymous-type-to-create-json-objects/</link>
		<comments>http://www.markhneedham.com/blog/2009/05/31/sharpcouch-use-anonymous-type-to-create-json-objects/#comments</comments>
		<pubDate>Sun, 31 May 2009 10:59:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[sharpcouch]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1275</guid>
		<description><![CDATA[I&#8217;ve been playing around with CouchDB a bit today and in particular making use of SharpCouch, a library which acts as a wrapper around CouchDB calls. It is included in the CouchBrowse library which is recommended as a good starting point for interacting with CouchDB from C# code. I decided to work out how the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with <a href="http://wiki.apache.org/couchdb/FrontPage">CouchDB</a> a bit today and in particular making use of <a href="http://code.google.com/p/couchbrowse/source/browse/trunk/SharpCouch/SharpCouch.cs">SharpCouch</a>, a library which acts as a wrapper around CouchDB calls. It is included in the <a href="http://code.google.com/p/couchbrowse/">CouchBrowse</a> library which is recommended as a good starting point for interacting with CouchDB from C# code.</p>
<p>I decided to work out how the API worked with by writing an integration test to save a document to the database.</p>
<p>The API is reasonably easy to understand and I ended up with the following test:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ShouldAllowMeToSaveADocument<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var server <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://localhost:5984&quot;</span><span style="color: #008000;">;</span>
    var databaseName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;sharpcouch&quot;</span><span style="color: #008000;">;</span>
    var sharpCouchDb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SharpCouch<span style="color: #008000;">.</span><span style="color: #0000FF;">DB</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sharpCouchDb<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateDocument</span><span style="color: #008000;">&#40;</span>server, databaseName, <span style="color: #666666;">&quot;{ key : <span style="color: #008080; font-weight: bold;">\&quot;</span>value<span style="color: #008080; font-weight: bold;">\&quot;</span>}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In theory that should save the JSON object { key = &#8220;value&#8221; } to the database but it actually throws a 500 internal error in <a href="http://code.google.com/p/couchbrowse/source/browse/trunk/SharpCouch/SharpCouch.cs">SharpCouch.cs</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>275
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">HttpWebResponse resp <span style="color: #008000;">=</span> req<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> HttpWebResponse<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Debugging into that line the Status property is set to &#8216;Protocol Error&#8217; and a bit of Googling led me to think that I probably had a <a href="https://issues.apache.org/jira/browse/COUCHDB-335">malformed client request</a>.</p>
<p>I tried the same test but this time created the document to save by creating an anonymous type and then converted it to a JSON object using the <a href="http://litjson.sourceforge.net/">LitJSON</a> library:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ShouldAllowMeToSaveADocumentWithAnonymousType<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var server <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://localhost:5984&quot;</span><span style="color: #008000;">;</span>
    var databaseName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;sharpcouch&quot;</span><span style="color: #008000;">;</span>
    var sharpCouchDb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SharpCouch<span style="color: #008000;">.</span><span style="color: #0000FF;">DB</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var savedDocument <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> key <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;value&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    sharpCouchDb<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateDocument</span><span style="color: #008000;">&#40;</span>server, databaseName, JsonMapper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToJson</span><span style="color: #008000;">&#40;</span>savedDocument<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>That works much better and does actually save the document to the database which I was able to verify by adding a new method to SharpCouch.cs which creates a document and then returns the &#8216;documentID&#8217;, allowing me to reload it afterwards.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ShouldAllowMeToSaveAndRetrieveADocument<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var server <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://localhost:5984&quot;</span><span style="color: #008000;">;</span>
    var databaseName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;sharpcouch&quot;</span><span style="color: #008000;">;</span>
    var sharpCouchDb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SharpCouch<span style="color: #008000;">.</span><span style="color: #0000FF;">DB</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var savedDocument <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span>key <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;value&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    var documentId <span style="color: #008000;">=</span> sharpCouchDb<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateDocumentAndReturnId</span><span style="color: #008000;">&#40;</span>server, databaseName, JsonMapper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToJson</span><span style="color: #008000;">&#40;</span>savedDocument<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var retrievedDocument <span style="color: #008000;">=</span> sharpCouchDb<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDocument</span><span style="color: #008000;">&#40;</span>server, databaseName, documentId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>savedDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">key</span>, JsonMapper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToObject</span><span style="color: #008000;">&#40;</span>retrievedDocument<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;key&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> CreateDocumentAndReturnId<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> server, <span style="color: #6666cc; font-weight: bold;">string</span> db, <span style="color: #6666cc; font-weight: bold;">string</span> content<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var response <span style="color: #008000;">=</span> DoRequest<span style="color: #008000;">&#40;</span>server <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;/&quot;</span> <span style="color: #008000;">+</span> db, <span style="color: #666666;">&quot;POST&quot;</span>, content, <span style="color: #666666;">&quot;application/json&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> JsonMapper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToObject</span><span style="color: #008000;">&#40;</span>response<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;id&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;m not sure how well anonymous types work for more complicated JSON objects but for the simple cases it seems to do the job.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/05/31/sharpcouch-use-anonymous-type-to-create-json-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

