TweetSaver.com Launched on MongoDB, MongoMapper

As you all probably well know by now, we’re big fans of MongoDB here at Squeejee. We’ve written about it, spoken about it, and ported mysql based apps over to it.

We’ve recently launched our first application built from the ground up on MongoDB. TweetSaver also happens to be the first production app that we know of utilizing John Nunemaker’s MongoMapper ORM.

How MongoDB Made Things Easy

Look, Ma, no schema!

Mongo’s schema-less nature made it easy to store all sorts of data from a number of APIs. Grabbing rich, deep data structures was easy because we didn’t have to relationally model the relationships between documents and subdocuments. We could simply ‘stash the hash.’

Upserts!

One of the coolest features of MongoDB is the ‘upsert.’ In ActiveRecord we do this with find_or_create_by_xxx. This performs two calls — a select and insert — for new records. MongoDB allows you to peform fire-and-forget saves by upserting:

myColl.update( { name: "Joe" }, { name: "Joe", age: 20 }, { upsert: true } );

where the name ‘Joe’ is your unique field.

How MongoDB Made Things Hard

Embedded object, foreign key, or DBRef?

One of the toughest things about going NO-SQL is having to think about the best way to model data for your application. Although MongoDB shines in storing deep, nested document structures, it also fully supports more relational database design. This choice means that you need to think through the tradeoffs up front. It’s a good idea to go through the scenarios in which you’ll consume pieces of data and determine how to store it.

Plugins / Gems built on ActiveRecord

Since MongoMapper is not ActiveRecord, all of those Rails gems and plugins that have ActiveRecord dependencies will not work. Your choices here are:

(a) Go with a MySQL hybrid approach to store a subset of data in MySQL. For example, we store our Users table in MySQL to take advantage of the number of Authentication plugins out there. (RestfulAuth / AuthLogic / TwitterAuth/ etc). However, even this is pretty simple to implement with MongoDB. Here is a Gist from John Nunemaker to implement authentication with his MongoMapper ORM: http://gist.github.com/147427

(b) Port over a solution to work with MongoDB. The nice part about this route is that there are no migrations to futz with. Just define your needed fields directly in your app, plugin, or gem.

Major Gotcha’s

It’s easy to play around with Mongo, see its speed and think that the laws of computing have been suspended. Alas, collections with hundreds of thousands of records still require indexing.

So, before you start your next project take a minute to see if MongoDB might be a good fit… it’s been a great addition to the Squeejee toolbox.

  •