MongoDB with PostgreSQL in One Rails App MongoDB with PostgreSQL in One Rails App mongodb mongodb

MongoDB with PostgreSQL in One Rails App


You don't need to disable ActiveRecord to use MongoDB. Check out Mongoid and just add the gem plus any models along side any of your existing ActiveRecord models. You should note that MongoHQ is just a hosting service for MongoDB and can be used alongside any Object Document Mapper (ODM).

For further details check http://mongoid.org/en/mongoid/docs/installation.html. Just skip the optional 'Getting Rid of Active Record' step.


On a recent client site I worked with a production system that merged MySQL and MongoDB data with a single Java app. To be honest, it was a nightmare. To join data between the two databases required complex Java data structures and lots of code, which is actually databases do best.

One use-case for a two database system is to have the pure transactional data in the SQL database, and the aggregate the data into MongoDB for reporting etc. In fact this had been the original plan at the client, but along the way the databases became interrelated for transactional data.

The system has become so difficult to maintain that is is planned to be scrapped and replaced with a MongoDB-only solution (using Meteor.js).

Postgres has excellent support for JSON documents via it's jsonb datatype, and it is fully supported under Rails 4.2, out of the box. I have also worked with this and I find it a breeze, and I would recommend this approach.

This allows an easy mix of SQL and NoSQL transactions, eg

select id, blast_results::json#>'{"BlastOutput2","report","results","search","hits"}' from blast_caches where id in (select primer_left_blast_cache_idfrom primer3_output_pairs where id in (185423,185422,185421,185420,185419) )

It doesn't offer the full MongoDB data manipulation features, but probably is enough for most needs.

Some useful links here:
http://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails
https://dockyard.com/blog/2014/05/27/avoid-rails-when-generating-json-responses-with-postgresql

There are also reports that it can outperform MongoDB on json:
http://www.slideshare.net/EnterpriseDB/the-nosql-way-in-postgres

Another option would be to move your Rails app entirely to MongoDB, and Rails has very good support for MongoDB.

I would not recommend running two databases, based on personal observations on how it can go bad.