我有一个包含2个字段的表格:
table documents
docu_id uuid
attachments jsonb
attachments jsonb
列的示例数据为:
[
{
"size": 10,
"attach_id": "d3a21f904068"
},{
"Size": 0.143,
"attach_id": "5ba4b285565b"
}
]
我已经看到了许多有关基于字段名称更新/删除jsonb的示例,但是可以从匿名数组中删除匿名对象,其中{ {1}}
"attach_id" = "X" and "docu_id"="Y":
答案 0 :(得分:0)
好吧,找到了解决方案,所以我在这里分享它((上个月的链接http://rextester.com/YICZ86369):
create table documents(docu_id text, attachments jsonb);
insert into documents values
('001',
'[
{
"name": "uno",
"id":"1"
},
{
"name": "dos",
"id":"2"
},
{
"name": "tres",
"id":"3"
}
]'
),
('002',
'[
{
"name": "eins",
"id":"1"
},
{
"name": "zwei",
"id":"2"
}
]'
);
select * from documents;
UPDATE documents
SET attachments = attachments #-
array(
SELECT i
FROM generate_series(0, jsonb_array_length(attachments) - 1) AS i
WHERE (attachments->i->'id' = '"2"')
)::text[] /* cast as text */
where docu_id = '002';
select * from documents;