使用 PostgreSQL 删除 NESTED JSONB 数组中的对象
Delete objects inside NESTED JSONB arrays with PostgreSQL
json请求:
INSERT INTO test.demotbl (data)
VALUES ('{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"id": "RO",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "salin"
}
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}'::jsonb)
问题 1:如何从 y1 数组中删除 "id":"RO" 的数组值?
y1 json数组中可以没有任何元素我想根据id条件删除数组值。
如何从 y1 数组中删除 "id":"RO" 的数组值?
y1 json数组中可以没有任何元素我想根据id条件删除数组值。
删除后的预期输出:
{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}
问题 2:如何只从 y1 数组中删除 "id":"RO"
预期输出:
{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "salin"
}
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}
编辑说明:我误解了这个问题。以为您想删除包含 'RO' 的整个对象。我编辑了答案以便删除 id。
您提供的jsonb对象有一个小错误。它应该看起来像这样:
{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"id": "RO",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "salin"
}]
},
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}
话虽如此,这应该可行,但请记住 - 这将替换工作 table 中的所有条目。 jsonb 对象在 "field".
字段中
with zd as (select ('{x4,y1,'||index-1||',id}')::text[] as path
from table
,jsonb_array_elements((field->>'x4')::jsonb->'y1')
with ordinality arr(x,index)
where x->>'id'='RO'
)
update table set field=
field #- zd.path
from zd
此致,
比亚尼
json请求:
INSERT INTO test.demotbl (data)
VALUES ('{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"id": "RO",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "salin"
}
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}'::jsonb)
问题 1:如何从 y1 数组中删除 "id":"RO" 的数组值? y1 json数组中可以没有任何元素我想根据id条件删除数组值。
如何从 y1 数组中删除 "id":"RO" 的数组值? y1 json数组中可以没有任何元素我想根据id条件删除数组值。
删除后的预期输出:
{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}
问题 2:如何只从 y1 数组中删除 "id":"RO"
预期输出:
{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "salin"
}
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}
编辑说明:我误解了这个问题。以为您想删除包含 'RO' 的整个对象。我编辑了答案以便删除 id。
您提供的jsonb对象有一个小错误。它应该看起来像这样:
{
"x1": "Americas",
"x2": "West",
"x3": [{
"x_id": "sam"
}],
"x4": {
"a1": true,
"a2": false,
"a3": [
"xx",
"xx"
],
"a4": [
"Josh"
],
"y1": [{
"id": "RW",
"z2": true,
"z3": "USER",
"z4": [{
"name": "john"
}]
},
{
"id": "RO",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "salin"
}]
},
{
"id": "DBA",
"z2": false,
"z3": "SELECT",
"z4": [{
"name": "Samule"
}]
}
]
}
}
话虽如此,这应该可行,但请记住 - 这将替换工作 table 中的所有条目。 jsonb 对象在 "field".
字段中with zd as (select ('{x4,y1,'||index-1||',id}')::text[] as path
from table
,jsonb_array_elements((field->>'x4')::jsonb->'y1')
with ordinality arr(x,index)
where x->>'id'='RO'
)
update table set field=
field #- zd.path
from zd
此致,
比亚尼