Last active
January 23, 2020 14:21
-
-
Save guilhermebkel/a33b90d4bc98e14586541b69b6ee17ec to your computer and use it in GitHub Desktop.
Updating single value inside json on postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
-> 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