如何在postgres jsonb查询中检查键的值是否为真

Moj*_*vin 4 sql postgresql json boolean

例如我的表是:

CREATE TABLE mytable (
    id bigint NOT NULL,
    foo jsonb
);
Run Code Online (Sandbox Code Playgroud)

它有一些价值:

id   | foo
-----+-------
 1   | "{'a':false,'b':true}"
 2   | "{'a':true,'b':false}"
 3   | NULL
Run Code Online (Sandbox Code Playgroud)

我想知道如何检查键的值是否为true,我应该使用哪个运算符

我想要这样的东西,可以检查价值:

SELECT 1 
FROM mytable
WHERE
id=2
AND
foo['a'] is true
;
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 5

语法foo['a']在 Postgres 中无效。

如果要访问键的值,则需要使用手册中记录->>运算符

select *
from mytable
where id = 2
and foo ->> 'a' = 'true';
Run Code Online (Sandbox Code Playgroud)