Postgresql – How to work with JSONB column

jsonpostgresql

I use Postgres and jsonB column

I have the following table:

id(VAARCHAR(64)) | value(JSONB)
--------------------------------
1                |[{"x": "10", "y": 1}, {"x": "32110", "y": 12}] 

X has always unique

I can add a new element in this sequence.

I can check that sequence contains the element with x value "10" or not.

But I can't
**Remove element with the special value of x (remove value which contains x:"10")?

Find the maximum value of y?**

Can anybody help me with this?

Best Answer

Is a remove

 UPDATE table_name
    set jsonB_column_name=(SELECT jsonb_agg(z.a)
                                   FROM  (SELECT jsonb_array_elements(t.jsonB_column_name) as a
                                          FROM   table_namet
                                          where id='3a-a7ed-bf88dcdde0ab') z
                                   WHERE NOT a @> '{"x":"10"}')
    where id='1';

Is a search

select max(c.jsonb->>'y')
from (SELECT jsonb_array_elements(t.jsonB_column_name) as jsonb
      FROM   table_namet) as c;