« Back

Move to Mongo

by Andrew Zeneski  •  published July 30
 

As an experiment, I decided to switch the document store from Redis to MongoDB.

First I wrote a migration script to pull the current blog post data out of Redis, restructured it since MongoDB supports lists and embedded documents inside a main document. Because of this restricting a lot of code was removed and is now a lot cleaner. Also, instead of having to do several queries to the document store to retrieve posts, it is now handled completely by a single query.

entry = self.mongo.posts.find_one({'slug': slug})

I created several indices to the post document for the normal lookups sorts:

db.posts.ensureIndex({'front_page' : 1})
db.posts.ensureIndex({'created' : -1})
db.posts.ensureIndex({'slug' : 1})

The front_page flag is a boolean to indicate if a post is promoted to the front page of the blog or not. The created field is a time stamp and is used to sort in descending order. The slug field is the "search engine friendly" string that is used in the URLs.

A simple lookup for the home page and right navigation now looks like this:

posts = self.mongo.posts.find({'front_page': True}).sort("created", -1)[0:15]

Simply it finds all posts that has the boolean field True, sorts them by the created date descending and limits the result to the first 15 entries.

Switching to Mongo was really just an exercise, but it does seem like a more natural way to store blog/article content. No longer do I need to populate a bunch of lists and sets with Redis keys to categories and tag posts. MongoDB has a very powerful query language that supports looking at contents of an embedded list, values on an embedded object, fast sorts and indexed lookups.

Finally, I added the [MarkItUp] (http://markitup.jaysalvat.com "MarkItUp")+ jQuery editor plugin to the compose screen. A clean editor with Markdown support and a very nice preview pane.

It's almost as if I am now running Blog v2.0.

// ttfn

« Back