Skip to content

Instantly share code, notes, and snippets.

@jaz303
Created February 1, 2022 15:42
Show Gist options
  • Save jaz303/5a8af4a9e07f0fe1782be12ac7b73d35 to your computer and use it in GitHub Desktop.
Save jaz303/5a8af4a9e07f0fe1782be12ac7b73d35 to your computer and use it in GitHub Desktop.
json mutate
DROP TABLE IF EXISTS student;
CREATE TABLE student (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
state JSONB
);
INSERT INTO student (name, state) VALUES ('Bob', '{"completed": []}');
UPDATE student SET state = jsonb_set(state, '{completed}', state->'completed' || '[1,2,3]', TRUE) WHERE name = 'Bob';
UPDATE student SET state = jsonb_set(state, '{completed}', state->'completed' || '[4,5]', TRUE) WHERE name = 'Bob';
SELECT * FROM student;
@jaz303
Copy link
Author

jaz303 commented Feb 1, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment