jsonb

How do you create a Postgresql JSONB array in array index?

时光总嘲笑我的痴心妄想 提交于 2019-12-22 12:32:02
问题 I have structure like this: user_id, a. a is of type jsonb and has the following structure: { b: [ {ids: [1,2,3,4]}, {ids: [2,3,4]}, {ids: [1,2,4]}, ... ] } How would I make an index that enabled me to find all users (user_id) that has a certain id in the ids list? 回答1: Is a GIN index what you want? It seems that you first need to organized the IDs into a form that is more tractable. I'm more familiar with Python than I am with the PostgreSQL ways of manipulating JSON, so I used PL/Python for

Postgresql jsonb traversal

一个人想着一个人 提交于 2019-12-22 11:35:55
问题 I am very new to the PG jsonb field. I have for example a jsonb field containing the following { "RootModule": { "path": [ 1 ], "tags": { "ModuleBase1": { "value": 40640, "humanstring": "40640" }, "ModuleBase2": { "value": 40200, "humanstring": "40200" } }, "children": { "RtuInfoModule": { "path": [ 1, 0 ], "tags": { "in0": { "value": 11172, "humanstring": "11172" }, "in1": { "value": 25913, "humanstring": "25913" } etc.... Is there a way to query X levels deep and search the "tags" key for a

Remove jsonb array element by value

故事扮演 提交于 2019-12-22 04:22:12
问题 I did figure out how to remove a value from an array for a single record, but how to do it for many of them. The problem is in the way how I use the subquery. As it has to return only single element. Maybe my approach is wrong. Given input: '{attributes:['is_new', 'is_old']}' Expected result '{attributes: ['is_old']}' #remove 'is_new' from jsonb array Real example: # sku | properties # -------+-------------------------------- # nu3_1 | { + # | "name": "silly_hodgkin", + # | "type": "food", +

Operator does not exist: json = json

久未见 提交于 2019-12-21 06:56:20
问题 when I try to select some record from a table SELECT * FROM movie_test WHERE tags = ('["dramatic","women", "political"]'::json) The sql code cast a error LINE 1: SELECT * FROM movie_test WHERE tags = ('["dramatic","women",... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. ********** 错误 ********** ERROR: operator does not exist: json = json SQL 状态: 42883 指导建议:No operator matches the given name and argument type(s). You might need to

Rails and jsonb type “jsonb” does not exist

前提是你 提交于 2019-12-21 03:09:13
问题 psql --version psql (PostgreSQL) 9.4.1 rails -v Rails 4.2.0 I added a jsonb column through migration like that class AddPreferencesToUsers < ActiveRecord::Migration def change add_column :users, :preferences, :jsonb, null: false, default: '{}' add_index :users, :preferences, using: :gin end end I get this error : PG::UndefinedObject: ERROR: type "jsonb" does not exist LINE 1: SELECT 'jsonb'::regtype::oid any help ? 回答1: After looking around I discovered that my postgresql version is not 9.4

JOOQ How to convert JSON based on other column value?

十年热恋 提交于 2019-12-20 04:06:36
问题 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

JOOQ How to convert JSON based on other column value?

扶醉桌前 提交于 2019-12-20 04:05:44
问题 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

Update certain array elements of a json array in PostgreSQL 9.4

坚强是说给别人听的谎言 提交于 2019-12-19 19:44:00
问题 I have a table like this; CREATE TABLE test ( id BIGSERIAL PRIMARY KEY, data JSONB ); INSERT INTO test(data) VALUES('[1,2,"a",4,"8",6]'); -- id = 1 INSERT INTO test(data) VALUES('[1,2,"b",4,"7",6]'); -- id = 2 How to update element data->1 and data->3 into something else without PL/* ? 回答1: You cannot manipulate selected elements of a json / jsonb type directly. Functionality for that is still missing in Postgres 9.4 (see @Craig's comment). You have to do 3 steps: Unnest / decompose the JSON

Postgresql, retrieve value for specific key from json array

懵懂的女人 提交于 2019-12-19 07:52:49
问题 I have Postgres JSONB array of objects, looking like this : '[ { "skillId": "1", "skillLevel": 42 }, { "skillId": "2", "skillLevel": 41 } ]' This JSONB is a function argument. What is the most efficient way to retrieve skillLevel for skillId = "1" . I've tried to play with jsonb_array_elements but everything I've done so far is looking really messy. 回答1: with my_table(data) as ( values ('[ { "skillId": "1", "skillLevel": 42 }, { "skillId": "2", "skillLevel": 41 } ]'::jsonb) ) select elem->>

Postgresql, retrieve value for specific key from json array

对着背影说爱祢 提交于 2019-12-19 07:50:10
问题 I have Postgres JSONB array of objects, looking like this : '[ { "skillId": "1", "skillLevel": 42 }, { "skillId": "2", "skillLevel": 41 } ]' This JSONB is a function argument. What is the most efficient way to retrieve skillLevel for skillId = "1" . I've tried to play with jsonb_array_elements but everything I've done so far is looking really messy. 回答1: with my_table(data) as ( values ('[ { "skillId": "1", "skillLevel": 42 }, { "skillId": "2", "skillLevel": 41 } ]'::jsonb) ) select elem->>