Postgresql在jsonb对象中搜索值
我在一个表中有一个jsonb字段,其值为Postgresql在jsonb对象中搜索值,postgresql,jsonb,Postgresql,Jsonb,我在一个表中有一个jsonb字段,其值为 { "message": { "sender": { "from": "91**********" }, "channel": "some kind of text", "content": { "text": "some kind of text", "type": "text" }, "recipient": { "to": "91******
{
"message": {
"sender": {
"from": "91**********"
},
"channel": "some kind of text",
"content": {
"text": "some kind of text",
"type": "text"
},
"recipient": {
"to": "91**********",
"recipient_type": "some kind of text"
},
"preferences": {
"webHookDNId": "some kind of text"
}
},
"metaData": {
"version": "some kind of text"
}
}
现在我想搜索对象的“to”键中具有特定电话号码的所有此类值。我正在为此使用以下查询,但它不起作用
select * from table_name where (column1::jsonb ? '91**********') ;
您可以使用
->
和->
从键中提取值:
select *
from the_table
where (the_column -> 'recipient' ->> 'to') = '91**********';
或#>
运算符
select *
from the_table
where the_column #>> '{recipient,to}' = '91**********';
?
查找顶级密钥。您显示的JSON只有两个顶级键,“message”和“metadata”。当然,它们与“91*********”不匹配
您可能需要包含运算符@>
:
@> '{"message":{"recipient":{"to":"91**********"}}}'
这将由列上的任一类型的JSONB GIN索引支持