Postgresql json select from values in second layer of containment of arrays -
i have jsonb column 'data' contains tree json, example:
{ "libraries":[ { "books":[ { "name":"mybook", "type":"fiction" }, { "name":"yourbook", "type":"comedy" } { "name":"hisbook", "type":"fiction" } ] } ] }
i want able index using query selects value indented "book" jsons according type. book names fiction.
i able using jsonb_array_elements join query, understand not optimized using gin index. query is
select books->'name' data, jsonb_array_elements(data->'libraries') libraries, jsonb_array_elements(libraries->'books') books, books->>'type'='grading'
if example data showing type of data common in json, suggest may setting things wrong.
why not make library table , book table , not use json @ all, seems json not right choice here.
create table library ( id serial, name text ); create table book ( isbn bigint, name text, book_type text ); create table library_books ( library_id integer, isbn bigint ) select book.* library_books library_id = 1;
Comments
Post a Comment