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

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -