在postgresql中查询jsonb

在postgresql中查询jsonb,postgresql,Postgresql,我的postgresql数据库表中有一个名为authors的jsonb列,该列包含如下数据: [{"id":134,"nameKey":"smith, john"}, {"id":112,"nameKey":"bats, billy"}] 如何从jsonb author列包含id为112的author的表中返回所有行 我需要像这样的东西 select * from record where authors -> id = 2 我还需要通过姓名获得: select * from gar

我的postgresql数据库表中有一个名为authors的jsonb列,该列包含如下数据:

[{"id":134,"nameKey":"smith, john"},
 {"id":112,"nameKey":"bats, billy"}]
如何从jsonb author列包含id为112的author的表中返回所有行

我需要像这样的东西

select * from record
where authors -> id = 2
我还需要通过姓名获得:

select * from gardner_record
where authors -> nameKey like '%john%'
我发现postgresql jsonb文档几乎无法理解。当我尝试按ID选择时,它表示我缺少一个演员阵容。当我尝试按名称选择时,它表示该列不存在


我应该如何查询此列?

使用
jsonb\u数组\u元素

select t.*
    from t cross join jsonb_array_elements(authors) as j
    where j->>'id' = '112'



select t.*
    from t cross join jsonb_array_elements(authors) as j
    where j->>'nameKey' like '%john%'

这是jsonb中的数组还是jsonb[]类型?它最初是文本列,但我想查询它,所以我将类型更改为jsonb。我应该设置类型jsonb[]吗?我是jsonb的新手,找不到任何合适的教程。没关系。我想你必须使用安全壳操作员
@>
从我观看演示的作者的记录中选择*,它似乎同时选择了两个元素。@AvinKavish:是的,如果只想选择匹配的元素,可以使用
从..
中选择j。看见但是OP似乎想要表中的列,所以澄清一下,在没有连接的where子句中,运算符中没有写的look?i、 e.
其中数组包含与谓词匹配的元素