Wednesday, 31 May 2017

Updating object inside an array of objects postgres

I'm trying to update a json array with objects inside and i want to update just one object each time.

I have [{"ifname": "lte0", "enabled": "1"}, {"ifname": "lte1", "enabled": "0"}, {"ifname": "lte2", "enabled": "0"}, {"ifname": "radio0", "disabled": "1"}]

And for example, I want to update ifname lte0 to have "enabled" = 0.

My idea was to do a query to get this array and then iterate with a for loop and compare each key "ifname" and whose "enabled" with my parameter to change.

"SELECT data->>'interfaces' FROM device_services WHERE device_Id = $1

How can i update directly for instance {"ifname": "lte0", "enabled": "1"} to {"ifname": "lte0", "enabled": "0"} ?

i only know to do this..

client.query("UPDATE device_services SET data=jsonb_set(data::jsonb,'{interfaces}',$1::jsonb,true) WHERE device_id=$2", [data, deviceId], function (err, result) {

But i dont want to update all data field because it has another values...



via Catia Matos

No comments:

Post a Comment