I
I
Ivanoff-i2016-03-18 11:45:23
PostgreSQL
Ivanoff-i, 2016-03-18 11:45:23

How to add a value to jsonb, provided that there is no such value in it?

The task is next. You need to add a value to the jsonb field, like [2,7,835,824,26] another number, provided that it is not there. In other words, add a value to the set in those records that do not have this value in the set.
So far I've come up with this:

UPDATE public.table SET jsonb_field = jsonb_set(jsonb_field, '{1000000000}','4') WHERE jsonb_field NOT IN (SELECT jsonb_field FROM public.table WHERE jsonb_field @> '4');

Maybe there are simpler and more correct options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2016-03-18
@kshvakov

remove the select, why NOT IN (SELECT...?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question