Storing nested Hashes in PostgreSQL with Rails 4 (and Hstore) Storing nested Hashes in PostgreSQL with Rails 4 (and Hstore) postgresql postgresql

Storing nested Hashes in PostgreSQL with Rails 4 (and Hstore)


You should try using JSON extension for Postgresql. It will do exactly what you want: validate and store JSON. Originally JSON extension was added in 9.2. Postgres 9.3 added more features for JSON extension including new operators and functions. And postgres 9.4 will have advanced indexing support for JSON so you'll be futureproof with this setup.

Related links:http://www.postgresql.org/docs/9.3/static/functions-json.htmlhttp://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.3#JSON:_Additional_functionality


Nested Hstore is a gem that supports nested hashes (and arrays and other types) in hstores, achieving something similar to a document store like MongoDB. It uses a hybrid of hstore and JSON serialization. It hasn't been tested on Rails 4, though.


As the documentation of Hstore states, keys and values in Hstore are simply text strings. Hstore cannot store multi-level json objects, nor is it meant for that purpose.

So to make it simple, you cannot replace MongoDb with PostgreSQL by simply using Hstore. Instead, you'll need to create tables for each kind of objects like in any other relational databases. If the schema of your objects are very dynamic, it'd be better to keep using MongoDB.