MongoDB with MongoMapper and Ruby on Rails

I'm sure we've all heard the pros & cons of the NoSQL movement so these will not be covered here. I've been experimenting with a number of alternatives to RDBMS for a while such as CouchDB, TokyoCabinet, Redis and recently MongoDB.

MongoDB (from "humongous") is a scalable, high-performance, open source, schema-free, document-oriented database. Written in C++, MongoDB features:

MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS (which provide structured schemas and powerful queries).

- from the MongoDB site.

The reason I've settled on MongoDB is (for me) it's been the most useful, more so than straight KV stores such as Redis and Tokyo. Technically CouchDB is very similar to MongoDB being a document store too, however MongoDB seems to 'gel better' (that's a technical term).

To be able to use this MongoDB goodness in my Rails app, I'm making use of the MongoMapper gem as it's the most popular one (github forks and watchers metric). There are other ORMs which you can use with MongoDB, Mongoid seems like a good alternative and the rest are documented here: http://www.mongodb.org/display/DOCS/Ruby+Language+Center.

Installation

Download and run mongod http://www.mongodb.org/display/DOCS/Downloads

Install the gems:

Configure your environment, and remove AR:

Add a config file for your database:

And add an initializer to setup MongoMapper and friends:

Getting stuff done

Now I know what you might be thinking, 'oh no not another ORM all my favorite gems won't work!', that is not the case! (*in some cases)

Usage

These Models are stripped down but you get the idea. Let say you're using Devise, then your User model could look like this:

And if that user had comments the model could look like:

These are just to help kick-start your app development, for more complete examples see the open source apps and http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails :

Open Source Ruby Applications using MongoDB (and MongoMapper)

NewsMonger - A simple social news application demonstrating MongoDB and Rails


Oupsnow - A bugtracker in Rails/MongoMapper
http://github.com/shingara/oupsnow/

Watchtower - An example app built with Sinatra, Mustache and MongoDB
http://github.com/kneath/watchtower/

Shapado - stackoverflow like question and answer site: 
http://gitorious.org/shapado/shapado

mmmblog - a blogging engine by the same guys as Shapado (http://blog.ricodigo.com/)
http://gitorious.org/mmmblog

More at http://wiki.github.com/jnunemaker/mongomapper/projects-using-mongomapper

Pro Tips

  • MongoMapper uses a fork of the validatable gem - which has some differences with AR validations
  • Denormalization is needed to reduce round-trips to the DB
  • Change how you think of your data models - this is closer to an object store
  • The finders have nifty options checkout http://wiki.github.com/jnunemaker/mongomapper/whats-new (0.5.7)
  • Don't be afraid to drop down and use to the Ruby MongoDB driver directly http://www.mongodb.org/display/DOCS/Ruby+Tutorial
  • Keep an eye out on the github commit feed as it's a bit of a moving target still

Summary
No migrations (yes!), storing arrays and hashes as attribute keys (sweet), and inplace updates (ftw). We're not going to use MongoDB to build transactional systems any time soon, but for the majority of web applications it's a perfect fit.


Bit Zesty - Ruby on Rails Development (and MongoDB) UK

Loading mentions Retweet
Filed under  //  development   mongodb   nosql   ruby on rails  
Comments (9)

Web Development Terminology

While we are not big fans of the technical jargon, sometimes we just cannot help it. I will cover some of the key terms used on our site.

 

Web Application (webapp)

A web application is computer software that is accessible through the Internet. Social networks, e-commerce sites and wikis are just some examples of web applications.

  • Web applications enable you to deliver and update software without the need to distribute and install it.  This makes it easier to reach new users and keep them happy.
  • It is also not dependant on the user’s operating system, meaning you cover a wider market without having to make changes to your software.

Ruby on Rails (RoR)

Ruby on Rails is a framework for creating web applications using the Ruby programming language.

 

  • It enables applications to be built rapidly. Therefore you receive more features at lower cost.
  • It encourages good development practices, resulting in higher quality code. This ensures your application is extendable and scalable.
  • By having excellent support for automated testing, it allows for robust code, which reduces the amount of maintenance required.

Agile Development

Agile is a development methodology, which embraces collaboration with customers, face-to-face communication, self-organizing teams, frequent delivery and ability to respond to changing requirements.

  • Agile enables you to re-prioritise or even introduce new features at any point, allowing us to react to changes in your business and ensuring that the application is just as you wanted it to be.

Test Driven Development (TDD)

Test Driven Development is a technique where automated tests are created before the program code. This forces code to be written and improved upon until it passes the test.

  • Test Driven Development enables to eliminate the majority of bugs, ensuring you get software that is robust and properly functioning.

This list is by no means complete and I am intending to keep updating it especially as we add new terminology to our website.

Bit Zesty Ltd

Loading mentions Retweet
Filed under  //  agile   development   ruby on rails  
Comment (1)