I want to get all records where any value in jsonb field contains a text regardless of key.
For example: field contains json {k1: 'hello', k2: 'world'}
. I want to get this record by the text 'hello'. I don't care about keys or any json structure, only values.
One dirty hack is to cast the field to varchar where jsonb_field::varchar like ...
, but this is ugly and it will match keys and {} as well.
Another dirty hack looks like this:
SELECT * FROM mytable AS m1 WHERE (
SELECT string_agg(value, '') FROM jsonb_each_text( (SELECT name FROM mytable AS m2 WHERE m1.id= m2.id)::jsonb )
) LIKE '%whatever%';
but it's ugly too.
How can I do this?