<?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>source code bean &#187; Mono</title>
	<atom:link href="http://sourcecodebean.com/archives/category/mono/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>thoughts and ideas from a .net developer</description>
	<lastBuildDate>Fri, 27 Jan 2012 20:36:28 +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>MongoDB, C# and Mono</title>
		<link>http://sourcecodebean.com/archives/mongodb-c-and-mono/1408</link>
		<comments>http://sourcecodebean.com/archives/mongodb-c-and-mono/1408#comments</comments>
		<pubDate>Wed, 11 Jan 2012 18:53:48 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1408</guid>
		<description><![CDATA[MongoDB is one of the many free open source NoSQL databases that exist today. I wanted to try out how well the official drivers for C# worked when using Mono. On the MongoDB web site they have pre built binaries of MongoDB for almost every platform, i grabbed the 64 bit binary for OSX. No [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mongodb.org/">MongoDB</a> is one of the many free open source NoSQL databases that exist today. I wanted to try out how well the official drivers for C# worked when using Mono. </p>
<p>On the MongoDB web site they have pre built binaries of MongoDB for almost every platform, i grabbed the 64 bit binary for OSX. No installation is required, just unzip the file and run the &#8220;mongod&#8221; binary from the directory. There is also a binary called &#8220;mongo&#8221;, this is the <a href="http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell">mongo interactive shell</a> which is very useful. </p>
<p>The C# drivers are available, both in binary form and as source, from the <a href="http://www.mongodb.org/display/DOCS/CSharp+Language+Center">C# language center</a>. To get started I fired up MonoDevelop 2.8 running on Mono 2.10.8:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/01/monodevelop.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/01/monodevelop-300x187.png" alt="" title="monodevelop" width="300" height="187" class="aligncenter size-medium wp-image-1451" /></a><br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/01/edit_references.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/01/edit_references-300x187.png" alt="" title="edit_references" width="300" height="187" class="aligncenter size-medium wp-image-1449" /></a><br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/01/add_references.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/01/add_references-300x201.png" alt="" title="add_references" width="300" height="201" class="aligncenter size-medium wp-image-1450" /></a></p>
<p>MonoDevelop has matured a lot since I used it the first time. It is still not as good as Visual Studio, but it is getting there. What takes most time to get used to is the totally different set of shortcuts, and the lack of all ReSharper magic. </p>
<p>Let me break down a simple console application example on how to use MongoDB using the official driver from 10gen: <span id="more-1408"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Collections</span>.<span class="me1">Generic</span>;</div>
</li>
<li class="li2">
<div class="de2">using System.<span class="me1">Linq</span>;</div>
</li>
<li class="li1">
<div class="de1">using MongoDB.<span class="me1">Bson</span>;</div>
</li>
<li class="li1">
<div class="de1">using MongoDB.<span class="me1">Driver</span>;</div>
</li>
<li class="li1">
<div class="de1">using MongoDB.<span class="me1">Driver</span>.<span class="me1">Builders</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">namespace MongoDbTest</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; public class Article</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public MongoDB.<span class="me1">Bson</span>.<span class="me1">ObjectId</span> id <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; public <span class="kw4">string</span> title <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">string</span> status <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> tags <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span>&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; class MainClass</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> <span class="kw4">void</span> Main <span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="co1">// Connect to the database and get a colleciton</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; MongoServer server = MongoServer.<span class="me1">Create</span> <span class="br0">&#40;</span><span class="st0">&quot;mongodb://localhost&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; MongoDatabase db = server.<span class="me1">GetDatabase</span> <span class="br0">&#40;</span><span class="st0">&quot;mongodb-tutorial&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Get an article collection</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; MongoCollection&lt;BsonDocument&gt; articles = db.<span class="me1">GetCollection</span>&lt;BsonDocument&gt; <span class="br0">&#40;</span><span class="st0">&quot;articles&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Empty the collection (so all runs of this program will result in the same output)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; articles.<span class="me1">RemoveAll</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Create two articles</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; articles.<span class="me1">Save</span> <span class="br0">&#40;</span>new BsonDocument</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;title&quot;</span>, <span class="st0">&quot;A MongoDB article&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;tags&quot;</span>, new BsonArray <span class="br0">&#123;</span> <span class="st0">&quot;c#&quot;</span>, <span class="st0">&quot;mongodb&quot;</span> <span class="br0">&#125;</span><span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; articles.<span class="me1">Save</span> <span class="br0">&#40;</span>new BsonDocument</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;title&quot;</span>, <span class="st0">&quot;A Mono article&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;draft&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;tags&quot;</span>, new BsonArray <span class="br0">&#123;</span> <span class="st0">&quot;c#&quot;</span>, <span class="st0">&quot;mono&quot;</span> <span class="br0">&#125;</span><span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<blockquote><p>If you type this in the MongoDB shell, you will see that the following articles are stored in the database:</p>
<pre>
> db.articles.find()
        {
          "_id" : ObjectId("4f097f8e74b5b343b97f56a7"),
          "title" : "A MongoDB article",
          "status" : "published",
          "tags" : [ "c#", "mongodb" ]
        }

        {
          "_id" : ObjectId("4f097f8e74b5b343b97f56a8"),
          "title" :
          "A Mono article",
          "status" : "draft",
          "tags" : [ "c#", "mono" ]
       }
</pre>
</blockquote>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="co1">// Find and print the first article</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; BsonDocument firstArticle = articles.<span class="me1">FindOne</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; PrintArticle <span class="br0">&#40;</span> <span class="co1">// Access the properties using indexing and then casting using the .As*</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstArticle<span class="br0">&#91;</span><span class="st0">&quot;_id&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsObjectId</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstArticle<span class="br0">&#91;</span><span class="st0">&quot;title&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsString</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstArticle<span class="br0">&#91;</span><span class="st0">&quot;status&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsString</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>firstArticle<span class="br0">&#91;</span><span class="st0">&quot;tags&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsBsonArray</span><span class="br0">&#41;</span>.<span class="me1">Select</span> <span class="br0">&#40;</span>x =&gt; x.<span class="me1">AsString</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Typed collections&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Using indexing and the .As* operations is not that convenient. A better way</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// is to use a typed collection. Earlier we defined a Article class, lets use it:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; MongoCollection&lt;Article&gt; typedArticles = db.<span class="me1">GetCollection</span>&lt;Article&gt; <span class="br0">&#40;</span><span class="st0">&quot;articles&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Article typedArticle = typedArticles.<span class="me1">FindOneAs</span>&lt;Article&gt; <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; PrintArticle <span class="br0">&#40;</span>typedArticle.<span class="me1">id</span>, typedArticle.<span class="me1">title</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; typedArticle.<span class="me1">status</span>, typedArticle.<span class="me1">tags</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Save a typed article</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; typedArticles.<span class="me1">Save</span> <span class="br0">&#40;</span>new Article <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = <span class="st0">&quot;An article about MonoDevelop&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status = <span class="st0">&quot;published&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags = new <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#123;</span><span class="st0">&quot;c#&quot;</span>, <span class="st0">&quot;mono&quot;</span>, <span class="st0">&quot;mono-develop&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Querying</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Find article with status draft</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Article draftArticle = typedArticles</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">FindOneAs</span>&lt;Article&gt; <span class="br0">&#40;</span>Query.<span class="me1">EQ</span> <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;draft&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; PrintArticle <span class="br0">&#40;</span>draftArticle.<span class="me1">id</span>, draftArticle.<span class="me1">title</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; draftArticle.<span class="me1">status</span>, draftArticle.<span class="me1">tags</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="co1">// Updating</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Lets update the draft article and set status published</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;Nr of published articles before update:&quot;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ typedArticles.<span class="me1">Find</span> <span class="br0">&#40;</span>Query.<span class="me1">EQ</span> <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">Count</span> <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; typedArticles.<span class="me1">Update</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; new QueryDocument <span class="br0">&#123;</span> <span class="br0">&#123;</span> <span class="st0">&quot;_id&quot;</span>, draftArticle.<span class="me1">id</span> <span class="br0">&#125;</span> <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; new UpdateDocument <span class="br0">&#123;</span> <span class="br0">&#123;</span> <span class="st0">&quot;$set&quot;</span>, new BsonDocument <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// $set is atomic</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;Nr of pubhlished articles after update:&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ typedArticles.<span class="me1">Find</span> <span class="br0">&#40;</span>Query.<span class="me1">EQ</span> <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">Count</span> <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// MapReduce</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// This will count the number of times a tag has been used by using MapReduce</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; var map =</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;function() {&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;this.tags.forEach(function(t) {&quot;</span> + <span class="co1">// Iterate all the tags and </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp; &nbsp; &nbsp;emit(t, { count : 1 }); &quot;</span> + &nbsp; &nbsp;<span class="co1">// emit the tag name and initial count </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;})&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;}&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; var reduce =</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;function(key, emits) {&quot;</span> + <span class="co1">// Reduce by tag name and summarize the result</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;total = 0;&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;for (var i in emits) {&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp; &nbsp; &nbsp;total += emits[i].count;&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;}&quot;</span> +</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;return { count : total };&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;}&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;MapReduce result:&quot;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; var mr = typedArticles.<span class="me1">MapReduce</span> <span class="br0">&#40;</span>map, reduce<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var document in mr.<span class="me1">GetResults</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span>document.<span class="me1">ToJson</span> <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> <span class="kw4">void</span> PrintArticle <span class="br0">&#40;</span>ObjectId id, <span class="kw4">string</span> title, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">string</span> status, IEnumerable&lt;string&gt; tags<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;id: &quot;</span> + id + <span class="st0">&quot;, title: &quot;</span> + title<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;- tags: &quot;</span> + <span class="kw4">string</span>.<span class="me1">Join</span> <span class="br0">&#40;</span><span class="st0">&quot;, &quot;</span>, tags<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Output from the program:</p>
<blockquote>
<p>id: 4f0989fc74b5b3458ae38105, title: A MongoDB article<br />
- tags: c#, mongodb<br />
id: 4f0989fc74b5b3458ae38105, title: A MongoDB article<br />
- tags: c#, mongodb<br />
id: 4f0989fc74b5b3458ae38106, title: A Mono article<br />
- tags: c#, mono<br />
Nr of pubhlished articles before update:2<br />
Nr of pubhlished articles after update:3<br />
MapReduce result:<br />
{ &#8220;_id&#8221; : &#8220;c#&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 3.0 } }<br />
{ &#8220;_id&#8221; : &#8220;mongodb&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 1.0 } }<br />
{ &#8220;_id&#8221; : &#8220;mono&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 2.0 } }<br />
{ &#8220;_id&#8221; : &#8220;mono-develop&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 1.0 } }
</p></blockquote>
<p>Using the C# driver for MongoDB, is as you can see, very straight forward. Usually when I run applications on Mono I make sure to build all libraries using the Mono compiler, to make sure the Microsoft compiler have not used any optimizations that is not supported under Mono (I have run into this issue in the past). However, in this case the .NET binaries on the MongoDB site seems to work just fine under Mono. I also downloaded the MongoDB C# driver source and build them using MonoDevelop without any problems. </p>
<p>One improvement I would like to see is LINQ support in the driver. It would be so much more convenient to be able to use LINQ and strongly typed objects to query the database or create MapReduce-functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/mongodb-c-and-mono/1408/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An update on the mono memory issue</title>
		<link>http://sourcecodebean.com/archives/an-update-on-the-mono-memory-issue/184</link>
		<comments>http://sourcecodebean.com/archives/an-update-on-the-mono-memory-issue/184#comments</comments>
		<pubDate>Wed, 13 May 2009 13:21:16 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=184</guid>
		<description><![CDATA[The workaround I tested a few weeks ago did not really solve my problem – actually it made it worse. The autorestart caused mono to hang and not restart at all, so my site stopped responding every 6th hour. I quickly had to disable this. Still I had the memory consumption problems. From various sources [...]]]></description>
			<content:encoded><![CDATA[<p>The workaround I tested a <a href="http://sourcecodebean.com/archives/mod-mono-server2-memory-consumption-problems">few weeks ago</a> did not really solve my problem – actually it made it worse. The autorestart caused mono to hang and not restart at all, so my site stopped responding every 6th hour. I quickly had to disable this. Still I had the memory consumption problems. From various sources I was told that the memory issues would be fixed in the recently released mono 2.4. Also I found a <a href="https://bugzilla.novell.com/show_bug.cgi?id=472732">bug report</a> regarding AutoRestart, which also should have been fixed in 2.4. So I decided to give Mono 2.4 a try.<br />
The problem was that there were no packages for Ubuntu 8.10, so I had to download the sources and build my own deb packages. I found this great <a href="http://go-mono.com/forums/#nabble-p22841086">blog post</a> that describes the process of building and installing Mono 2.4. </p>
<p>So far it seems to be working, but it is too early to say if the memory consumption issues are resolved. </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/an-update-on-the-mono-memory-issue/184/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>mod-mono-server2 memory consumption problems</title>
		<link>http://sourcecodebean.com/archives/mod-mono-server2-memory-consumption-problems/121</link>
		<comments>http://sourcecodebean.com/archives/mod-mono-server2-memory-consumption-problems/121#comments</comments>
		<pubDate>Tue, 31 Mar 2009 21:21:49 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=121</guid>
		<description><![CDATA[Lately I have been experiencing that the mod-mono-server2 process running on the server hosting the video upload web-service (I blogged about this in my last post) has been consuming a lot of memory. Once, it even reached the point where all memory (1.5GB) of the server was consumed and the Linux OOM-killer killed the mod-mono-server2 [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been experiencing that the mod-mono-server2 process running on the server hosting the video upload web-service (I blogged about this in my last post) has been consuming a lot of memory. Once, it even reached the point where all memory (1.5GB) of the server was consumed and the Linux OOM-killer killed the mod-mono-server2 process. At this point apache failed to restart it. If you are interested you can see the kernel log <a href='http://sourcecodebean.com/wp-content/uploads/2009/03/kern.log'>here</a> and the apache log <a href='http://sourcecodebean.com/wp-content/uploads/2009/03/apache.log'>here</a>. The mono version I am using is mono-apache-server2 1.9.1-2 (Ubuntu 8.10 Intrepid).</p>
<p>I googled the issue and found some information on the <a href="http://www.mono-project.com/Mod_mono">mod_mono</a> page:</p>
<blockquote><p><strong>Under high load, mono process consumes a lot of memory, website stops responding</strong><br />
These symptoms have been reported, but their underlying causes are not known. Set the MonoAutoRestartMode, MonoAutoRestartRequests, MonoMaxActiveRequests, and MonoMaxWaitingRequests directives as described earlier to limit the lifetime of the mono process and to restrict the concurrency happening in the server. </p></blockquote>
<p>The above describes my issue pretty well, except for the &#8220;under high load&#8221; part. The video transcoding service is still in beta and is only used by one customer so far. It serves around 500 requests a day, which is not a lot, so I wouldn&#8217;t expect this kind of behavior. My guess is that every time a file is uploaded using the UploadFile webmethod additional memory is allocated, but not properly released/reused by mono. </p>
<p><del datetime="2009-05-13T13:26:11+00:00">As a workaround for this I will have the mod-mono-server2 process restart restart every 6th hour. This can be done by adding the following lines to <code>/etc/apache2/mods-enabled/mod_mono.conf</code>:</del> Update: This did not work, caused my site to hang entirely every 6th hour. </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">MonoAutoRestartMode <a href="http://www.php.net/time"><span class="kw3">Time</span></a></div>
</li>
<li class="li1">
<div class="de1">MonoAutoRestartRequests <span class="nu0">00</span>:<span class="nu0">06</span></div>
</li>
</ol>
</div>
<p>Hopefully this will limit the memory usage, for now. I would appreciate feedback from anyone who have experienced similar problems on mono. </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/mod-mono-server2-memory-consumption-problems/121/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Building a distributed web service using Amazon Web Services</title>
		<link>http://sourcecodebean.com/archives/building-a-distributed-web-service-using-amazon-web-services/93</link>
		<comments>http://sourcecodebean.com/archives/building-a-distributed-web-service-using-amazon-web-services/93#comments</comments>
		<pubDate>Sun, 22 Mar 2009 18:18:08 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Amazon Web Services]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=93</guid>
		<description><![CDATA[A few months ago my employer asked me if it would be possible to create a web service for encoding videos. I had been playing around with Amazon&#8217;s web services for a while, and it seemed like the perfect foundation for building this. I decided to build the backend in Python and use ffmpeg for [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago my employer asked me if it would be possible to create a web service for encoding videos. I had been playing around with Amazon&#8217;s web services for a while, and it seemed like the perfect foundation for building this.</p>
<p>I decided to build the backend in Python and use ffmpeg for encoding movies. I looked into building the web service frontend in Python as well, but the SOAP libraries I could find for Python did not seem very mature or maintained. Instead I started thinking about building it in ASP.NET (I had previous experiences from building web services in ASP.NET). After some research and testing with Apache and Mono (I wanted to use Linux VMs only) I decided to develop the frontend in ASP.NET but host it on Apache.</p>
<p><span id="more-93"></span><br />
To make the service scalable I decided to break it down into several parts and use message passing between the different parts. The parts I broke it down into are:</p>
<ul>
<li>Web service frontend – what the user calls to encode a movie. Implemented in ASP.NET, hosted on Apache/Mono on Linux.</li>
<li>Encode Worker – A python process managing the encoding of videos.</li>
<li>Encode Master – Manages number of running Encode Workers. Implemented in Python.</li>
</ul>
<p>When a movie gets uploaded to the web service frontend it gets placed into the encode queue. The encode workers periodically checks if there is anything in the queue, and if it is encodes it. The Encode Master manages the number of running Encode Workers (based on the current length of the encode queue). If the queue growes to long, we just fire up a few new VMs running the worker. </p>
<p> This is a schematic view of how the service has been implemented and how the different components are related to the Amazon services:</p>
<p><img class="size-large wp-image-92" title="Video Encoding Service" src="http://sourcecodebean.com/wp-content/uploads/2009/03/dqcvideo-1024x535.png" alt="Video Encoding Service" width="650" /></p>
<p>Right now we are implementing the solution for our first customer, pretty existing I must say! In an upcoming post I will discuss the different libraries for Python and ASP.NET I used for communicating with the Amazon Web Services.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/building-a-distributed-web-service-using-amazon-web-services/93/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: sourcecodebean.com @ 2012-02-06 06:32:11 -->
