Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB with PostgreSQL in One Rails App

Can I use MongoDB and a PostgreSQL in one rails app? Specifically I will eventually want to use something like MongoHQ. So far I have failed to get this working in experimentation. And it concerns me that the MongoDB documentation specifically says I have to disable ActiveRecord. Any advice would be appreciated.

like image 356
Mark Lybrand Avatar asked Jun 20 '12 07:06

Mark Lybrand


2 Answers

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.

like image 74
Kevin Sylvestre Avatar answered Nov 08 '22 05:11

Kevin Sylvestre


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_id
from 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.

like image 36
port5432 Avatar answered Nov 08 '22 03:11

port5432