For several years the talk have been increasing rapidly about non-relational schema-less databases, and I have wanted to try one out. For Tathkarti.com, I started with regular PostgreSQL, but I soon started collecting large amounts of data I wanted to analyze. I thought this would be a perfect place to test out a non-relational database. Less than a year after, I found my self faced with the task of restructuring and expanding my whole application and database. There is a lot of undergoing changes, and I needed something agile as I keep testing different options. Even Rails migrations did not seem so friendly for the task, so I decided to move everything over to a non-relational database.
One advise I got from Yehuda Katz at Railsconf 11 was to try out Redis. However, as promising as it is, I found that MongoDB has a gem, Mongoid, that better suits my needs, and will make the migration and code changes less drastic. My decision was also strongly influenced by the fact that Mongoid already supports Sunspot Solr through Sunspot Mongoid, and is supported by Rails Admin, or so I thought at the time, until I realized Rails Admin does not work with it. The idea of nested documents sounded appealing as well, rather than relating documents that won’t be used but through their relative parent document anyways. The migration is still going on, so I have no records to show for, but so far I hold no regrets, despite some short backs I will explain shortly. I would still like to give Redis a go for some future project though.
Now to get started with Mongoid, you first need to understand what MongoDB is and how it works. For me, I got MongoDB: The Definitive Guide book to get me started. You don’t need to go through the whole book. Just the first 7-8 chapters to get going with just the development at least. The book is good for introduction, but it had some erratas that made me waste an hour or two cursing at the screen. So if you decide to get it, take a look at the book erratas once you feel something is wrong. If you’re an eBook hugger like me, you can get it DRM free from O’Reilly.
Next is Mongoid. I first went through a demo by Durran at RubyJax. It’s about 80 minutes long, and it serves as a nice introductory to both MongoDB and Mongoid. You will get some ideas about how to structure your data, when to use embedded documents and when to use relational ones and so. Then, there is always Railscasts with Ryan Bates.
This should have you ready to get going, and you can refer to the documentation for any assistance. One thing to note however is there are two formats to query and work with Mongoid. One is used all over the Mongoid documentation, and it goes like this:
Person.where(last_name: "Jordan")
I prefer to use the regular symbol form I’m used to in Rails.
Person.where(:last_name => "Jordan")
The main problem I faced during the migration was with Dates, which are not supported yet for querying, and you will need to convert all Date fields to Time. The other problem is sorting or querying for pre-epoch years (before 1970), which was not possible until recently with the MongoDB 2 release. I’m yet to test that part though, as I might have to upgrade Mongoid to the latest release, and this might break some other gems.