Blog An exploration of the art and
craft of software development

Playing with MongoDB

Posted by Marty Haught on Sunday, January 17, 2010

I finally took the plunge and started playing with MongoDB this last week. I’ve had it on my list to dive into for months now but I’ve been so busy that it kept slipping. I dabbled with CouchDB on a couple projects in 2009 and found it too slow for my needs but was really intrigued by the notion of building a system that uses a document store for its back-end.

There are several things that really attract me to using something like this for building new systems. First is the ability to store complex data structures together and not be forced to make tables because that how’s you have to play in relational systems. Second is the schema-less nature of the system. If I’m constantly changing the attributes of my models I won’t have to keep generating migrations and changing the schema. It turns out that you can’t just change everything willy-nilly and not have consequences. For example, you do need to figure out which models will be top level collections and which models will be nested under another. But it’s close enough as I use a seed script to regenerate my starting data. This way I can blow away the database and start over within seconds.

This month’s Boulder Ruby Meeting is on NOSQL systems. MongoDB and Tokyo Cabinet will be featured though the meeting is open for others to talk about other systems such as Redis, Cassandra, etc. I’m looking forward to this meeting especially since I’ve been toying with Mongo for several days now. I hope to have a more length post on things that I’ve learned in a few weeks.

Installing MongoDB

I’ll post my basic install steps for my MacBookPro running 10.5. I’m still using MacPorts so I just used the simple port command to get me going:

sudo port install mongodb

I did have to manually set up a symlink for a library that was missing:

sudo ln -s /opt/local/lib/nspr/libnspr4.dylib /opt/local/lib/

Since I’m not doing a custom install of MongoDB at this point I also created the data directory:

/data/db

The MongoDB site has lots of great documentation that you should refer to on how to get started. I won’t repeat it here.

Ruby Library

There’s a few choices on how to integrate your Ruby code with MongoDB. For now I’ve decided to go with Mongoid. So far things have been going well but it’s too early to say as I’m learning Mongo as well as the adapter at the same time. I’ll definitely have an update on this part in the future.

blog comments powered by Disqus