-
-
Save miheykrug/7172ef52d1a4fa3408012d4fb97e8572 to your computer and use it in GitHub Desktop.
Rails-5 postgresql-9.6 jsonb queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query | |
| http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails | |
| #payload: [{"kind"=>"person"}] | |
| Segment.where("payload @> ?", [{kind: "person"}].to_json) | |
| #data: {"interest"=>["music", "movies", "programming"]} | |
| Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json) | |
| Segment.where("data #>> '{interest, 1}' = 'movies' ") | |
| Segment.where("jsonb_array_length(data->'interest') > 1") | |
| Segment.where("data->'interest' ? :value", value: "movies") | |
| Segment.where("data -> 'interest' ? :value", value: ['programming']) | |
| data: {"customers"=>[{:name=>"david"}]} | |
| Segment.where("data #> '{customers,0}' ->> 'name' = 'david' ") | |
| Segment.where("data @> ?", {"customers": [{"name": "david"}]}.to_json) | |
| Segment.where("data -> 'customers' @> '[{\"name\": \"david\"}]'") | |
| Segment.where(" data -> 'customers' @> ?", [{name: "david"}].to_json) | |
| #data: {"uid"=>"5", "blog"=>"recode"} | |
| Segment.where("data @> ?", {uid: '5'}.to_json) | |
| Segment.where("data ->> 'blog' = 'recode'") | |
| Segment.where("data ->> 'blog' = ?", "recode") | |
| Segment.where("data ? :key", :key => 'uid') | |
| Segment.where("data -> :key LIKE :value", :key => 'blog, :value => "%recode%") | |
| #tags: ["dele, jones", "solomon"] | |
| # get a single tag | |
| #Segment.where("'solomon' = ANY (tags)") | |
| # which segments are tagged with 'solomon' | |
| Segment.where('? = ANY (tags)', 'solomon') | |
| # which segments are not tagged with 'solomon' | |
| Segment.where('? != ALL (tags)', 'solomon') | |
| # or | |
| Segment.where('NOT (? = ANY (tags))', 'solomon') | |
| #multiple tags | |
| Segment.where("tags @> ARRAY[?]::varchar[]", ["dele, jones", "solomon"]) | |
| # tags with 3 items | |
| Segment.where("array_length(tags, 1) >= 3") | |
| # SUM (Thanks @skplunkerin) | |
| https://gist.github.com/mankind/1802dbb64fc24be33d434d593afd6221#gistcomment-2711098 | |
| `https://stackoverflow.com/a/39280048/1180523` | |
| ``` | |
| #data: [{"amount"=>12.0},{"amount"=>25.50},{"amount"=>17.99}] | |
| Segment.select("SUM((data ->> 'amount')::FLOAT) AS total_amount") | |
| ``` | |
| # find if keys exists | |
| # https://dba.stackexchange.com/questions/196604/find-rows-containing-a-key-in-a-jsonb-array-of-records | |
| Presentation::Page.unscoped.where("EXISTS (SELECT 1 FROM jsonb_array_elements(template_data) elem WHERE elem ?| array [:keys])", keys: ['feedback_form', 'feedback_section']) | |
| # if locations is array of hashes | |
| # data [{"name": "Location 1", kind: "house"},{"name": "Location 2", kind: "house"}, {"name": "Location 3", kind: "office"}] | |
| User.where('locations @> ?', [{ kind: 'office' }].to_json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment