jsonb

PostgreSQL jsonb value in WHERE BETWEEN clause

痞子三分冷 提交于 2019-12-02 07:27:19
I've got jsonb field in my database table (a_table) with int value within, say: { "abc":{ "def":{ "ghk":500 } } } I'm about to create SELECT with filter by this field ("ghk") using WHERE clause: SELECT * FROM a_table WHERE ghk BETWEEN 0 AND 1000; How should i create such a query? Couldn't find good tutorial for jsonb usage so far. Thanks in advance! EDIT I found this solution: SELECT * FROM a_table WHERE a_field #> '{abc,def,ghk}' BETWEEN '0' AND '10000' ; Is it correct? The #> returns a JSONB document which you cannot cast to an int . You need the #>> operator which returns a scalar value

JOOQ How to convert JSON based on other column value?

落花浮王杯 提交于 2019-12-02 05:24:59
Let's say I have a table customer(int id, type varchar, preferences jsonb) . The type can be REGULAR , PREMIUM etc. Based on the column type value the preferences JSON structure will be different. While loading a customer record from database if type=REGULAR I want to convert it into RegularCustomerPreferences object type, and if type=PREMIUM I want to convert it into PremiumCustomerPreferences object type. I have gone through several tutorials on using JOOQ JSON converters/Bindings..but they are one to one mapping and not conditional based (depending on another column value). What is the

Postgres/JSON - update all array elements

帅比萌擦擦* 提交于 2019-12-02 03:25:39
问题 Given the following json: { "foo": [ { "bar": true }, { "bar": true } ] } How can I select the following: { "foo": [ { "bar": false }, { "bar": false } ] } ? So far I've figured out how to manipulate a single array value: SELECT jsonb_set( '{ "foo": [ { "bar": true }, { "bar": true } ] }'::jsonb, '{foo,0,bar}', to_jsonb(false) ) But how do I set all elements within an array? 回答1: There is no standard function to update json array elements by key. A custom function is probably the simplest way

How to reorder array in JSONB type column

僤鯓⒐⒋嵵緔 提交于 2019-12-02 03:24:51
In PostgreSQL table, a JSONB type column, and the value stored inside is an array [3,6,78,1] . What should I do to reorder it like [1,3,6,78] ? Unnest the array with jsonb_array_elements() and aggregate its sorted elements using jsonb_agg() : with the_data(val) as (values ('[3,6,78,1]'::jsonb)) select jsonb_agg(elem order by elem) as val from the_data cross join lateral jsonb_array_elements(val) as arr(elem); val --------------- [1, 3, 6, 78] (1 row) You can use the query in a custom function which will be handy in more complex queries: create or replace function jsonb_sort_array(jsonb)

Postgres/JSON - update all array elements

£可爱£侵袭症+ 提交于 2019-12-02 00:12:58
Given the following json: { "foo": [ { "bar": true }, { "bar": true } ] } How can I select the following: { "foo": [ { "bar": false }, { "bar": false } ] } ? So far I've figured out how to manipulate a single array value: SELECT jsonb_set( '{ "foo": [ { "bar": true }, { "bar": true } ] }'::jsonb, '{foo,0,bar}', to_jsonb(false) ) But how do I set all elements within an array? There is no standard function to update json array elements by key. A custom function is probably the simplest way to solve the problem: create or replace function update_array_elements(arr jsonb, key text, value jsonb)

In Django 1.9, what's the convention for using JSONField (native postgres jsonb)?

為{幸葍}努か 提交于 2019-12-01 02:46:52
Django highly suggests not to use null=True for CharField and TextField string-based fields in order not to have two possible values for "no data" (assuming you're allowing empty strings with blank=True ). This makes total sense to me and I do this in all my projects. Django 1.9 introduces JSONField , which uses the underlying Postgres jsonb data type. Does the suggestion above carry over to JSONField (i.e. blank=True should be used instead of null=True )? Or, should null=True be used instead? Or, should default=dict be used instead? Or, ..? Why? In other words, what is the convention for the

How to convert postgresql 9.4 jsonb to object without function/server side language

China☆狼群 提交于 2019-11-30 19:05:33
Is it possible to transform postgresql 9.4 jsonb data without creating function and without using any server side programming language? CREATE TABLE test (id SERIAL PRIMARY KEY,data JSONB); INSERT INTO test(data) VALUES('{"a":1,"b":2}'); INSERT INTO test(data) VALUES('{"a":3,"b":4,"c":7}'); INSERT INTO test(data) VALUES('{"a":5,"b":5,"d":8}'); SELECT * FROM test; id | data ----+------------------------- 1 | {"a": 1, "b": 2} 2 | {"a": 3, "b": 4, "c": 7} 3 | {"a": 5, "b": 5, "d": 8} to transform it into: {1:[1,2,null,null],2:[3,4,7,null],3:[5,5,null,8]} Erwin Brandstetter Use jsonb_populate

Rails (postgres) query with jsonb array

自闭症网瘾萝莉.ら 提交于 2019-11-30 17:53:00
问题 My Product model has a jsonb field specs (which we're managing using ActiveRecord's store_accessor ). Many of my products' specs have a spec in that hash called spec_options . Before now, this spec_option field was just text. Now it needs to be an array. The scope used before now to query products for this field was this: scope :with_spec_options, ->(spec_options) { where("'#{spec_options}'::jsonb \? (specs->>'spec_option')") } Ruby equivalent (just to help understand what this is doing):

Indexing jsonb data for pattern matching searches

跟風遠走 提交于 2019-11-30 17:46:31
问题 This is a follow-up to: Pattern matching on jsonb key/value I have a table as follows CREATE TABLE "PreStage".transaction ( transaction_id serial NOT NULL, transaction jsonb CONSTRAINT pk_transaction PRIMARY KEY (transaction_id) ); The content in my transaction jsonb column looks like {"ADDR": "abcd", "CITY": "abcd", "PROV": "", "ADDR2": "", "ADDR3": "","CNSNT": "Research-NA", "CNTRY": "NL", "EMAIL": "@.com", "PHONE": "12345", "HCO_NM": "HELLO", "UNQ_ID": "", "PSTL_CD": "1234", "HCP_SR_NM": "

Searching jsonb array in PostgreSQL

馋奶兔 提交于 2019-11-30 16:01:33
I'm trying to search a JSONB object in PostgreSQL 9.4. My question is similar to this thread . However my data structure is slightly different which is causing me problems. My data structure is like: [ {"id":1, "msg":"testing"} {"id":2, "msg":"tested"} {"id":3, "msg":"nothing"} ] and I want to search for matching objects in that array by msg (RegEx, LIKE, =, etc). To be more specific, I want all rows in the table where the JSONB field has an object with a "msg" that matches my request. The following shows a structure similar to what I have: SELECT * FROM (SELECT '[{"id":1,"msg":"testing"},{"id