使用Rails 4(和Hstore)在PostgreSQL中存储嵌套哈希 [英] Storing nested Hashes in PostgreSQL with Rails 4 (and Hstore)

查看:110
本文介绍了使用Rails 4(和Hstore)在PostgreSQL中存储嵌套哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,该应用程序聚集了Google API的许多数据。我目前将JSON响应存储在MongoDB中(因此我的Rails应用同时具有pg和mongo)。但是,今天,我遇到了PostgreSQL Hstore扩展,因此决定尝试一下。

I have a Rails app that aggregates a lot of data from Google API's. I store the JSON responses in MongoDB currently (so my Rails app has both pg and mongo). However, today, I've came across PostgreSQL Hstore extension, and I've decided to give it a try.

不幸的是,我遇到了一个问题。 API提供的JSON具有多个层次,因此 JSON.parse 之后的Ruby Hash包含哈希,其中包含新的哈希。但是, Hstore 是字符串键/值存储,并且深度仅为1级。因此,第一个哈希中的哈希只是变成字符串。

Unfortunately, I've ran into a problem. JSON given by API's is multiple levels deep, so Ruby Hash after JSON.parse contains hashes, which contain new hashes. However, Hstore is string key/value store, and it only goes 1 level deep. So hashes within first hash just become strings.

我发现要做的真正讨厌的黑客是 eval 变成字符串:

The really nasty hack I found to do is to eval the hashes that were turned into strings:

eval("{ "foo" => "bar" }")

我不喜欢这样。关于该怎么做的任何提示?我应该继续使用MongoDB,还是有更好的方法在PG中存储多级深哈希?

I do not like this. Any tips on what to do? Should I keep using MongoDB or is there any better way to store multi-level deep hashes in PG?

推荐答案

嵌套Hstore 是一种支持hstore中嵌套哈希(以及数组和其他类型)的gem,其实现的功能类似于文档像MongoDB这样的存储。它使用了hstore和JSON序列化的混合体。不过尚未在Rails 4上进行过测试。

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.

这篇关于使用Rails 4(和Hstore)在PostgreSQL中存储嵌套哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆