jsonb

How to query jsonb arrays with IN operator

一曲冷凌霜 提交于 2019-12-17 20:38:57
问题 I'm looking for a way to query postgres jsonb field with kind of "IN" clause inside an array. Let's assume I have a table CREATE TABLE test( id uuid, test_content jsonb, PRIMARY KEY(id) ); INSERT INTO test (id, test_content) VALUES ('aa82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"a","label1":"1"},{"label":"b","label1":"2"}]'), ('ba82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"c","label1":"3"}]'), ('da82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"d","label1":"4"}]'); I need to select

Pattern matching on jsonb key/value

ぐ巨炮叔叔 提交于 2019-12-17 20:27:30
问题 I am using PostgreSQL 9.4. My table has a jsonb column: CREATE TABLE "PreStage".transaction ( transaction_id serial NOT NULL, transaction jsonb CONSTRAINT pk_transaction PRIMARY KEY (transaction_id) ); CREATE INDEX idxgin ON "PreStage".transaction USING gin (transaction); I store transactions in terms of key / value in the JSONB column. One of the requirements is to search customer name from the key value, hence I am running a query like: SELECT transaction as data FROM "PreStage".transaction

Using indexes in json array in PostgreSQL

寵の児 提交于 2019-12-17 20:25:14
问题 Referring to the original stackoverflow question, I am trying to apply gin indexes to keys in objects of an array in Postgres 9.4 but I'm not getting the results as stated in the first answer. Can you please rectify the error? The steps I followed have been written below. Part 1: Creating table and indexes CREATE TABLE tracks (id serial, artists jsonb); CREATE INDEX tracks_artists_gin_idx ON tracks USING gin (artists); INSERT INTO tracks (id, artists) VALUES (1, '[{"name": "blink-182"}]');

postgresql migrating JSON to JSONB [duplicate]

我只是一个虾纸丫 提交于 2019-12-17 17:35:19
问题 This question already has answers here : Upgrade PostgreSQL JSON column to JSONB? (2 answers) Closed 3 years ago . In postgresql 9.4 the new JSONB was incorporated. On a live DB in postgresql 9.3 I have a JSON column. I want to migrate it to JSONB. Assuming I migrated the DB first to 9.4 (using pg_upgrade). What do I do next? 回答1: ALTER TABLE table_with_json ALTER COLUMN my_json SET DATA TYPE jsonb USING my_json::jsonb; 回答2: In the context of Rails, here is an ActiveRecord migration

How to perform update operations on columns of type JSONB in Postgres 9.4

跟風遠走 提交于 2019-12-17 03:24:14
问题 Looking through the documentation for the Postgres 9.4 datatype JSONB, it is not immediately obvious to me how to do updates on JSONB columns. Documentation for JSONB types and functions: http://www.postgresql.org/docs/9.4/static/functions-json.html http://www.postgresql.org/docs/9.4/static/datatype-json.html As an examples, I have this basic table structure: CREATE TABLE test(id serial, data jsonb); Inserting is easy, as in: INSERT INTO test(data) values ('{"name": "my-name", "tags": ["tag1"

Postgres flatten JSONB cell into row in view

跟風遠走 提交于 2019-12-14 04:25:54
问题 I have a simple postgres table: Column │ Type │ Modifiers ──────────────┼──────────────────────┼────────────────────── id │ integer │ not null default data │ jsonb │ Here's a simplified data structure for data : { "id": 3, "date": "2019-01-01", "well_report_table": [ {"element": "methane", "yield": 6, "price": 2.10 }, {"element": "pentane", "yield": 6, "price": 2.10 }, {"element": "butane", "yield": 6, "price": 3.50 } ], "cost_report_table": [ {"item": "fuel", "charge": 6.30 }, {"item":

Merge nested ARRAY of JSONB fields with a JOIN-like statement in PostgreSQL?

笑着哭i 提交于 2019-12-14 02:35:18
问题 Given this tables: BOOKS id | data ----+------------------------------------------------------- 1 | { title: 'Book 1', price: 10.5, authors: [{ id: 1}, { id: 2 }]} 2 | { title: 'Book 2', price: 11.5, authors: [{ id: 2 } } AUTHORS id | data -----+------------------------------------------------------- 1 | { name: 'Author 1', address: 'Address author 1' } 2 | { name: 'Author 2', address: 'Address author 2' } Is it possible to obtain this result, by merging the authors key array elements, with a

Using jsonb (PostgreSQL), how do I retrieve items with a certain value that's saved as an array?

Deadly 提交于 2019-12-14 02:23:13
问题 I'm trying to find all recipes in a certain category, where the key/value data is recorded in a PostgreSQL's jsonb column (and the value is saved as an array). For example, say I have "data" as my jsonb column in the "recipes" table, and I have three entries like so: "Recipe 1" contains a "data" with key/value of: "category_ids"=>[123, 4059] "Recipe 2" contains a "data" with key/value of: "category_ids"=>[405, 543] "Recipe 3" contains a "data" with key/value of: "category_ids"=>[567, 982] I

Sqlalchemy filter nested jsonb within arrays

匆匆过客 提交于 2019-12-13 17:14:24
问题 I have a Postgres JSONB field, with some nested arrays and other objects. from sqlalchemy.dialects.postgresql import JSONB class Family(db.Model): meta = db.Column(JSONB) joes = Family(meta=[ { "name": "Joe", "children": [ { "name": "Jane" }, { "name": "Kate" } ] }, { "name": "Lisa", "children": [ { "name": "Mary" }, { "name": "David" } ] }, ]) Is there a way to query all the kids with a certain substring in their names? If I want to query for 'a' it should get me Mary, David, Kate, Jane . I

How to sort objects in an array inside a json or jsonb value by a property of the objects?

纵然是瞬间 提交于 2019-12-13 15:10:01
问题 I have this pl/pgsql function to aggregate rows from two tables in a jsonb value ( data_table_1 and data_table_2 ). fk_id is a common foreign key id in both tables: DECLARE v_my_variable_1 jsonb; v_my_variable_2 jsonb; v_combined jsonb; BEGIN SELECT json_agg( data_table_1 ) INTO v_my_variable FROM data_table_1 WHERE fk_id = v_id; SELECT json_agg( data_table_2 ) into v_my_variable_2 FROM data_table_2 WHERE fk_id = v_id; SELECT v_my_variable || v_my_variable_2 into v_combined; Now I want to