From table rows in my PostgreSQL 12.8 database, I am trying to remove an object from an array, but instead it is deleting whole array of objects from the table.
settings
column holds below array of objects:
[
{
"id": 100,
"name": "testOne",
"settings": "settingOne"
},
{
"id": 101,
"name": testTwo,
"settings": "settingTwo"
},
]
I have three rows in the users
table with the column settings
of type jsonb
that holds an array of objects.
I want to delete the object with id = 101 for all users. I tried the below query:
update users
set settings =
jsonb_set(settings , '{settings}', (settings->'id') - (select distinct position-1 from users, jsonb_array_elements(settings)
with ordinality arr(elem, position) WHERE elem->>'id' = '101')::int)
By executing the above query it is deleting everything from the settings. How can I modify the above query in order to achieve the below result?
[
{
"id": 100,
"name": "testOne"
"settings": "settingOne"
}
]