postgresql Postgres JSONB从数组中删除嵌套对象

4ioopgfo  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(169)

我试图从JSONB(Postgresql 13)结构中的嵌套数组中删除特定对象
我想删除包含key:value 'id'的对象:138105}从'topTenUsersBySwims'和'topTenUsersByCalories'数组(可以作为两个单独的查询完成)
我能够获得对象的每个示例,但不确定如何在单个SQL查询中从数组中删除?
JSON结构如下

[{
        "year": 2023,
        "month": 5,
        "dailyStats": {},
        "totalSwims": 0,
        "totalUsers": 0,
        "dateUpdated": "Jun 01, 2023 12:01:45 AM",
        "totalCalories": 0,
        "totalDistance": 0,
        "totalNewUsers": 0,
        "totalSwimTime": 0,
        "topTenUsersBySwims": [{
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            }
        ],
        "topTenUsersByCalories": [{
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            }
        ]
    },
    {
        "year": 2023,
        "month": 3,
        "dailyStats": {
            "2": {
                "totalSwims": 1,
                "totalUsers": 1,
                "totalCalories": 112,
                "totalDistance": 400.0,
                "totalNewUsers": 0,
                "totalSwimTime": 492.1
            },
            "9": {
                "totalSwims": 2,
                "totalUsers": 1,
                "totalCalories": 210,
                "totalDistance": 750.0,
                "totalNewUsers": 0,
                "totalSwimTime": 975.8
            },
            "18": {
                "totalSwims": 1,
                "totalUsers": 1,
                "totalCalories": 112,
                "totalDistance": 400.0,
                "totalNewUsers": 0,
                "totalSwimTime": 465.3
            },
            "20": {
                "totalSwims": 1,
                "totalUsers": 1,
                "totalCalories": 659,
                "totalDistance": 2350.0,
                "totalNewUsers": 0,
                "totalSwimTime": 3307.2
            },
            "23": {
                "totalSwims": 1,
                "totalUsers": 1,
                "totalCalories": 140,
                "totalDistance": 500.0,
                "totalNewUsers": 0,
                "totalSwimTime": 540.1
            }
        },
        "totalSwims": 6,
        "totalUsers": 1,
        "dateUpdated": "Apr 01, 2023 12:01:44 AM",
        "totalCalories": 1233,
        "totalDistance": 4400.0,
        "totalNewUsers": 0,
        "totalSwimTime": 5780.5,
        "topTenUsersBySwims": [{
                "id": 138105,
                "name": "John Doe",
                "value": 6.0
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            }
        ],
        "topTenUsersByCalories": [{
                "id": 138105,
                "name": "John Doe",
                "value": 1233.0
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            },
            {
                "name": "--",
                "value": "--"
            }
        ]
    }
]
gupuwyp2

gupuwyp21#

可以这样做(使用操作符#-删除元素)

select id, 
json_data #- ('{1,topTenUsersByCalories,'||e.i-1/*index to remove*/||'}')::text[] as result  
from (  select id, 
        json_data #- ('{1,topTenUsersBySwims,'||e.i-1/*index to remove*/||'}')::text[] as json_data  
        from test4, jsonb_array_elements(json_data->1->'topTenUsersBySwims') with ordinality as e(cont, i)
        where e.cont->'id'='138105') t
, jsonb_array_elements(t.json_data->1->'topTenUsersByCalories') with ordinality as e(cont, i)
where e.cont->'id'='138105';

这将为您提供已修改的JSON数据,然后您可以执行更新以将更改应用于数据。Check json operators
工作样品

相关问题