Postgres JSON数据类型Rails查询

时间:2014-03-26 16:32:04

标签: json postgresql ruby-on-rails-4 rails-postgresql psql

我正在使用Postgres' json数据类型,但希望使用嵌套在json中的数据进行查询/排序。

我想在json数据类型上使用.where进行排序或查询。例如,我想查询拥有关注者数量>的用户500或者我想通过关注者或计数来订购。

谢谢!

示例:

model User

data: {
     "photos"=>[
       {"type"=>"facebook", "type_id"=>"facebook", "type_name"=>"Facebook", "url"=>"facebook.com"}
      ], 
     "social_profiles"=>[
         {"type"=>"vimeo", "type_id"=>"vimeo", "type_name"=>"Vimeo", "url"=>"http://vimeo.com/", "username"=>"v", "id"=>"1"},
         {"bio"=>"I am not a person, but a series of plants", "followers"=>1500, "following"=>240, "type"=>"twitter", "type_id"=>"twitter", "type_name"=>"Twitter", "url"=>"http://www.twitter.com/", "username"=>"123", "id"=>"123"}
     ]
}

3 个答案:

答案 0 :(得分:120)

对于任何偶然发现此事的人。我用ActiveRecord和Postgres'提出了一个查询列表。 JSON数据类型。随意编辑它以使其更清晰。

Postgres重要的rails命令:

# Sort based on the Hstore data:
2.1.1 :022 > Post.order("data->'hello' DESC")

=> #<ActiveRecord::Relation [#<Post id: 4, created_at: "2014-04-16 01:05:49", updated_at: "2014-04-16 01:05:49", data: {"hi"=>"23", "hello"=>"22"}>, #<Post id: 3, created_at: "2014-04-16 01:05:37", updated_at: "2014-04-16 01:05:37", data: {"hi"=>"13", "hello"=>"21"}>, #<Post id: 2, created_at: "2014-04-16 01:05:28", updated_at: "2014-04-16 01:05:28", data: {"hi"=>"3", "hello"=>"2"}>, #<Post id: 1, created_at: "2014-04-16 01:05:05", updated_at: "2014-04-16 01:05:05", data: {"hi"=>"2", "hello"=>"1"}>]> 



# Where inside a JSON object:
Record.where("data ->> 'likelihood' = '0.89'")

# Searching nested json object:
2.1.1 :130 > r.column_data
 => {"data1"=>[1, 2, 3], "data2"=>"data2-3", "array"=>[{"hello"=>1}, {"hi"=>2}], "nest"=>{"nest1"=>"yes"}} 

2.1.1 :130 > Record.where("column_data -> 'nest' ->> 'nest1' = 'yes' ")

# Searching within array:
Record.where("column_data #>> '{data1,1}' = '2' ")


# Searching within a value that’s an array:
Record.where("column_data #> '{array,0}' ->> 'hello' = '1' ")
# this only find for one element of the array. 

# All elements:
Record.where("column_data ->> 'array' LIKE '%hello%' ")
# This is advised against in rails, use below:
Record.where("column_data ->> 'array' LIKE ?", "%hello%")

<强>更新 我将很快写一篇博文,并将链接放在这里。

答案 1 :(得分:5)

根据这个http://edgeguides.rubyonrails.org/active_record_postgresql.html#json 使用->->>

有所不同
# db/migrate/20131220144913_create_events.rb
create_table :events do |t|
  t.json 'payload'
end

# app/models/event.rb
class Event < ActiveRecord::Base
end

# Usage
Event.create(payload: { kind: "user_renamed", change: ["jack", "john"]})

event = Event.first
event.payload # => {"kind"=>"user_renamed", "change"=>["jack", "john"]}

## Query based on JSON document
# The -> operator returns the original JSON type (which might be an object), whereas ->> returns text
Event.where("payload->>'kind' = ?", "user_renamed")

因此,您应该尝试使用Record.where("data ->> 'status' = 200 ")或适合您查询的运算符(http://www.postgresql.org/docs/current/static/functions-json.html)。

答案 2 :(得分:3)

您的问题似乎与您显示的数据相对应,但如果您的表名为usersdata是该表中的字段,其中JSON如{ {1}},然后是查询

{count:123}

会奏效。查看您的数据库模式,以确保您了解布局并检查查询是否有效,然后再将其与Rails约定复杂化。