<?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>Jb &#187; MongoDB</title>
	<atom:link href="http://jb55.com/category/programming/mongodb/feed/" rel="self" type="application/rss+xml" />
	<link>http://jb55.com</link>
	<description>I code</description>
	<lastBuildDate>Sun, 02 May 2010 17:41:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using MongoDB as a simple message board backend</title>
		<link>http://jb55.com/173/using-mongodb-as-a-simple-message-board-backend/</link>
		<comments>http://jb55.com/173/using-mongodb-as-a-simple-message-board-backend/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 06:26:13 +0000</pubDate>
		<dc:creator>jb55</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://jb55.com/?p=173</guid>
		<description><![CDATA[I&#8217;ve been implemented a wakaba-like message board using MongoDB as a storage backend and it has been a real breeze so far. Most interactions with the database have been one liners in Python and there is little work involved with inserting new threads or replies. I&#8217;d have to say the worse part of the whole [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been implemented a wakaba-like message board using MongoDB as a storage backend and it has been a real breeze so far. Most interactions with the database have been one liners in Python and there is little work involved with inserting new threads or replies. I&#8217;d have to say the worse part of the whole ordeal so far is the lack of integration with existing Python web frameworks. For that reason I decided not to use Django, which seems tightly coupled to sql solutions. I opted for a Werkzeug + Jinja2 + WTForms package called <a href="http://glashammer.org">Glashammer</a> which has worked quite well for me so far. Anyway, on to MongoDB&#8230;</p>
<h3>Database structure</h3>
<p>With my SQL habits my initial plan was to normalize all posts, with all threads and replies as MongoDB objects within a collection. It turns out this isn&#8217;t the best way to go about it. If the thread has a large amount of replies, the referenced lookup for each reply object is inefficient, and isn&#8217;t the MongoDBish way of doing things. What I found works best is a tree structure or a simple embedded array of replies:</p>
<pre><code class="javascript">{
  _id: ObjectId
  name: "jb55",
  topic: "What is this I don't even",
  msg: "I have important things to say",
  replies: [
    { name: "troll", msg: "tl;dr" },
    { name: "commentator", msg: "cool story bro" }
  ]
}</code></pre>
<p>As you can see replies are embedded in the thread object. This makes pulling the thread from the database very quick, since the data will be together on the disk. You&#8217;ll probably want the thread and replies to have a reference to a user object, or a simple email depending on how sophisticated you want to go. For the purposes of my 4chan/wakaba-like forum it made sense not to store a user reference.</p>
<h3>Some common tasks with PyMongo</h3>
<p>All interactions between Python and MongoDB are pretty smooth and it doesn&#8217;t take much work to get up and running with insertions and updates.</p>
<p><strong>Creating a new thread</strong></p>
<pre><code class="javascript">db.threads.insert(
{'name': 'jb55', 'topic': 'My important topic', 'msg': 'blah blah'})</code></pre>
<p><strong>Adding new replies to a thread</strong></p>
<pre><code class="javascript">reply = {'name': 'jb55', 'msg': 'This is my reply'}
db.threads.update({'_id': thread_id}, {'$push': {'replies': reply}})</code></pre>
<p><strong>Getting and displaying a thread</strong></p>
<pre><code class="javascript">thread = db.threads.find({'_id': thread_id})
data["thread"] = thread
return render_to_response('template.htm', **data)</code></pre>
<p>Easy isn&#8217;t it? No schemas, no hastle. In the interest of keeping this article short and sweet I think I&#8217;ll leave it at that for now. Try it out if you haven&#8217;t yet, it&#8217;s fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://jb55.com/173/using-mongodb-as-a-simple-message-board-backend/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
