我有jsonb数据
{
"a":[
{"b":1,"c":2,"d":3},
{"b":4,"c":5,"d":6}
],
"g":[
{"b":1,"c":2,"d":3},
{"b":4,"c":5,"d":6}
]
}
我想从“ a”和“ g”键的嵌套数组中删除c键。是否有单个查询来执行此操作?
答案 0 :(得分:1)
SELECT
jsonb_object_agg(key, a) -- 5
FROM (
SELECT
mydata,
key,
jsonb_agg(a_elems.value - 'c') as a -- 3/4
FROM
mytable,
jsonb_each(mydata) elems, -- 1
jsonb_array_elements(elems.value) AS a_elems -- 2
GROUP BY mydata, key -- 4
) s
GROUP BY mydata -- 5
c
元素的聚合JSON对象分隔开)-
运算符删除该元素。jsonb_agg()
重新聚集数组jsonb_object_agg()
重建原始JSON对象。