Skip to content

Instantly share code, notes, and snippets.

@guilhermebkel
Last active January 23, 2020 14:21
Show Gist options
  • Save guilhermebkel/a33b90d4bc98e14586541b69b6ee17ec to your computer and use it in GitHub Desktop.
Save guilhermebkel/a33b90d4bc98e14586541b69b6ee17ec to your computer and use it in GitHub Desktop.
Updating single value inside json on postgres
/*
-> Supposing there's a table called 'sel_collections' and a column 'info' with the following format:
{ "total_pictures": 2, "total_size": 100 }
-> After making the command below, the value inside this column will be:
{ "total_pictures": 3, "total_size": 100 }
*/
UPDATE
sel_collections
SET
info = jsonb_set(info, '{total_pictures}', (COALESCE(info->>'total_pictures','0')::int + 1)::text::jsonb)
WHERE
id=106;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment