Почему мой запрос JSONB не возвращает никакого результата

Посмотрите на мое поле JSONB

select internet_address from resporg_account_ids where id=3
=> [{"zone": "us-central1", "address": "127.0.0.1", "reserve": true}, {"zone": "us-east1", "address": "127.0.0.10", "reserve": true}

]

Соответствующий запрос JSONB

select * from resporg_account_ids where internet_address <@ '[{"zone": "us-central1", "address": "127.0.0.1", "reserve": true}]'::jsonb;

и это

select * from resporg_account_ids where internet_address <@ '{"zone": "us-central1", "address": "127.0.0.1", "reserve": true}'::jsonb;

И я не вижу результата в моей консоли вывода.

Кроме того, я хотел бы запросить его на основе отдельных ключей...

подобно

select * from resporg_account_ids where internet_address->>address="127.0.0.1" and internet_address->>reserve=true

В заключение,

jsondb=# select version();
                                                    version
---------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.10 on x86_64-apple-darwin17.2.0, compiled by Apple LLVM version 9.0.0 (clang-900.0.38), 64-bit
(1 row)

Замечания:

ЧАСТЬ 1: решена, спасибо @mroman за указание на это.

2 ответа

Вы должны использовать одинарные кавычки для ключей JSON объекта:

select * from resporg_account_ids where internet_address->>'address'='127.0.0.1'

Во второй части вы пытаетесь сравнить текст с логическим, потому что ->> возвращает текст Так что вам нужно привести тип:

... and (internet_address->>'reserve')::boolean is true

CTE приходит на помощь...

with intaddr as (select jsonb_array_elements(internet_address) internetaddr from resporg_account_ids)
select * from intaddr where internetaddr->>'address' = '127.0.0.1' and (internetaddr->>'reserve')::boolean=true;
Другие вопросы по тегам