I created a PostgreSQL (v10.0) table with a jsonb-array column as follows:
CREATE TABLE test (id INT, animals jsonb)
INSERT INTO test VALUES
(1, '["[monkeys, 10]", "[hamsters, 7]", "[foxes, 3]"]'),
(2, '["[monkeys, 10]", "[hamsters, 7]", "[foxes, 3]"]')
Then I want add new animals to the first row as follows:
UPDATE test
SET animals = animals || '["[hamsters, 7]", "[chicken, 2]"]'::jsonb
WHERE id = 1;
However, I want to append only those elements that are not yet in the array. In this case only [chicken, 2].