I have an array of json objects, from which I want to removed three specific objects. For example:
[{"id": "aaa"}, {"id": "aab"}, {"id": "aac"}, {"id": "aad"}]
I need to update the table and remove from the array the objects that contain "aaa"
, "aab"
, and "aac"
so that the array would only contain
[{"id": "aad"}]
or whatever other json objects that are stored in the array and don't contain the previous mentioned values stored in "id".
I'm using Postgres, and I know just that I need to use UPDATE, but that's about it (I'm still a beginner).
You need to unnest the array, filter out the unwanted elements and then aggregate back:
update the_table
set the_column = (select jsonb_agg(j)
from jsonb_array_elements(the_table.the_column) as t(j)
where j ->> 'id' not in ('aaa','aab','aac'));
Online example: https://rextester.com/HCJK50138
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.