jsonb

Add objects inside NESTED JSONB arrays with PostgreSQL

帅比萌擦擦* 提交于 2020-03-25 12:34:19
问题 Json Request INSERT INTO test.demotbl (data) VALUES ('{ "x1": "Americas", "x2": "West", "x3": [{ "x_id": "sam" }], "x4": { "a1": true, "a2": false, "a3": [ "xx", "xx" ], "a4": [ "Josh" ], "y1": [{ "id": "RW", "z2": true, "z3": "USER" }, { "id": "RO", "z2": false, "z3": "SELECT" } ] } }'::jsonb) I want to update a new filed z4 based on id condition "id": "RO".Eample "z4": [{ "name": "john" }, { "name": "Steve" } expected output : { "x1": "Americas", "x2": "West", "x3": [{ "x_id": "sam" }], "x4

Add/Update objects and elements inside NESTED JSONB arrays with PostgreSQL

旧城冷巷雨未停 提交于 2020-03-25 12:32:16
问题 Json request: INSERT INTO test.demotbl (data) VALUES ('{ "x1": "Americas", "x2": "West", "x3": [{ "x_id": "sam" }], "x4": { "a1": true, "a2": false, "a3": [ "xx", "xx" ], "a4": [ "Josh" ], "y1": [{ "id": "RW", "z2": true, "z3": "USER" }, { "id": "RO", "z2": false, "z3": "SELECT" } ] } }'::jsonb) I want to update the filed z4 based on id "id": "RO". I had similar use case here when i needed to update the z4 field used below query: with zd as (select ('{x4,y1,'||index-1||',z4}')::text[] as path

How do I search for a specific string in a JSON Postgres data type column?

我的梦境 提交于 2020-03-13 04:20:10
问题 I have a column named params in a table named reports which contains JSON. I need to find which rows contain the text 'authVar' anywhere in the JSON array. I don't know the path or level in which the text could appear. I want to just search through the JSON with a standard like operator. Something like: SELECT * FROM reports WHERE params LIKE '%authVar%' I have searched and googled and read the Postgres docs. I don't understand the JSON data type very well, and figure I am missing something

How do I search for a specific string in a JSON Postgres data type column?

六眼飞鱼酱① 提交于 2020-03-13 04:18:10
问题 I have a column named params in a table named reports which contains JSON. I need to find which rows contain the text 'authVar' anywhere in the JSON array. I don't know the path or level in which the text could appear. I want to just search through the JSON with a standard like operator. Something like: SELECT * FROM reports WHERE params LIKE '%authVar%' I have searched and googled and read the Postgres docs. I don't understand the JSON data type very well, and figure I am missing something

Best Way to Index All JSONB Fields With Boolean Values in PostgreSQL

坚强是说给别人听的谎言 提交于 2020-01-25 06:54:08
问题 I am creating a table that has a features jsonb column. There will be a dynamic set of features (each row can have an unknown set of features). Each feature is boolean true/false values. Example: Row 1: feature: { "happy": true, "tall": false, "motivated": true } Row 2: feature: { "happy": true, "fast": true, "strong": false } Row 3: feature: { "smart": true, "fast": true, "sleepy": false } What would be the best way to index this column such that I can make queries to find all rows with

Best Way to Index All JSONB Fields With Boolean Values in PostgreSQL

£可爱£侵袭症+ 提交于 2020-01-25 06:54:07
问题 I am creating a table that has a features jsonb column. There will be a dynamic set of features (each row can have an unknown set of features). Each feature is boolean true/false values. Example: Row 1: feature: { "happy": true, "tall": false, "motivated": true } Row 2: feature: { "happy": true, "fast": true, "strong": false } Row 3: feature: { "smart": true, "fast": true, "sleepy": false } What would be the best way to index this column such that I can make queries to find all rows with

efficiently group many fields including large text and jsonb

做~自己de王妃 提交于 2020-01-25 04:02:26
问题 Apologies in advance... long-winded question. Suppose I have a table table_x with 20 fields in it: table_x_id (identity pk) int1 int... int8 text1 text... text8 jsonb1 jsonb2 Now suppose I want to maintain rapid access to grouped data (say, fields int1, int2, text1_id, text2_id and jsonb1) in table_x . Call that Report 1 . Data doesn't really play an important role in posing this question, but here's an imaginary snippet from Report 1 : +-------------------------------------------------------

PostgreSQL - Add key to each objects of an JSONB array

梦想的初衷 提交于 2020-01-24 19:23:39
问题 My database contains a table which has a column with jsonb type , and I want to update a part of these data using functions/operators from postgreSQL . Given we have this: { "A":[ {"index":"1"}, {"index":"2"} ], "B":[ {"index":"3"}, {"index":"4"} ] } Let's say we went to add a key with an empty array to objects from "A" array, in order to have: { "A":[ {"index":"1", "myArray":[]}, {"index":"2", "myArray":[]} ], "B":[ {"index":"3"}, {"index":"4"} ] } How can I proceed? I've already tried this

PostgreSQL - Add key to each objects of an JSONB array

▼魔方 西西 提交于 2020-01-24 19:22:39
问题 My database contains a table which has a column with jsonb type , and I want to update a part of these data using functions/operators from postgreSQL . Given we have this: { "A":[ {"index":"1"}, {"index":"2"} ], "B":[ {"index":"3"}, {"index":"4"} ] } Let's say we went to add a key with an empty array to objects from "A" array, in order to have: { "A":[ {"index":"1", "myArray":[]}, {"index":"2", "myArray":[]} ], "B":[ {"index":"3"}, {"index":"4"} ] } How can I proceed? I've already tried this

Query postgres jsonb by value regardless of keys

我的梦境 提交于 2020-01-23 18:27:29
问题 I want to get all records where any value in jsonb field contains a text regardless of key. For example: field contains json {k1: 'hello', k2: 'world'} . I want to get this record by the text 'hello'. I don't care about keys or any json structure, only values. One dirty hack is to cast the field to varchar where jsonb_field::varchar like ... , but this is ugly and it will match keys and {} as well. Another dirty hack looks like this: SELECT * FROM mytable AS m1 WHERE ( SELECT string_agg(value