如何从每个元素 json 数组 postgres 中删除字段?

How to remove field from each elements json array postgres?

我有table

    CREATE TABLE items (
    id BIGINT PRIMARY KEY ,
    data jsonb
);

数据的格式

[
    {
        "id": "d20fe90c-137c-4713-bde1-2d4e75178ad3",
        "text": "text",
        "count": 1
    },
    {
        "id": "d20fe90c-137c-4713-bde1-2d4e75178ad4",
        ""text": "text",
        "count": 1
    }
]

如何从数据 json 数组的所有元素中删除字段 count

我试试

UPDATE items
SET data = data #- '{count}';

但是这个查询需要 count 之前的数组元素的索引 as

UPDATE items
    SET data = data #- '{0, count}';

没有运算符或内置函数可以做到这一点。取消嵌套数组并按如下方式聚合修改后的元素:

update items t
set data = (
    select jsonb_agg(elem- 'count')
    from items
    cross join lateral jsonb_array_elements(data) as arr(elem)
    where id = t.id)

db<>fiddle.

中测试