Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres and jsonb - search value at any key

Is possible to look for a given value at any key in a JSONB column in Postgres? In the documentation I can't see any example.

Example value at a JSONB column:

{
  a: 1,
  b: 2,
  c: 3
}

I want to find all records that have 1 as a value anywhere. NOTE: there may be other keys than a, b, c unknown at the moment.

like image 577
pmichna Avatar asked May 02 '17 13:05

pmichna


1 Answers

use value of jsonb_each_text, sample based on previous sample of McNets,:

t=# select * from json_test join jsonb_each_text(json_test.data) e on true 
where e.value = '1';
 id |                 data                 | key | value
----+--------------------------------------+-----+-------
  1 | {"a": 1}                             | a   | 1
  3 | {"a": 1, "b": {"c": "d", "e": true}} | a   | 1
(2 rows)
like image 137
Vao Tsun Avatar answered Oct 11 '22 17:10

Vao Tsun