从 jsonb 数组中删除元素

Bat*_*azi 4 postgresql jsonb

我有以下jsonb。我想从数组页面中删除名为“pageb”的元素。类似问题中提供的解决方案对我不起作用。

'{
  "data": {
    "id": "a1aldjfg3f",
    "pages": [
      {
        "type": "pagea"
      },
      {
        "type": "pageb"
      }                                
    ],
    "activity": "test"
  }
}'
Run Code Online (Sandbox Code Playgroud)

我的脚本现在看起来像这样。它不会返回任何错误,但不会删除元素。

  UPDATE database
  SET reports = jsonb_set(reports, '{data,pages}', (reports->'data'->'pages') - ('{"type":"pageb"}'), true)
  WHERE reports->'data'->'pages'  @> '[{"type":"pageb"}]';
Run Code Online (Sandbox Code Playgroud)

Anc*_*ron 6

以下是为可靠地删除数组中的元素而提供的答案和 PostgreSQL 使用数据修改WITH语句的能力的组合,但由于必要的相关性,它需要一个标识列(id在我的表中)才能工作:test

WITH new_reports AS (
    SELECT
        id,
        reports #- array['data','pages',(position - 1)::text] AS new_value
    FROM
        test,
        jsonb_array_elements(reports->'data'->'pages') WITH ORDINALITY arr(item, position)
    WHERE
        test.reports->'data'->'pages' @> '[{"type":"pageb"}]'
        AND
        item->>'type' = 'pageb'
    )
UPDATE test SET reports = new_reports.new_value FROM new_reports WHERE test.id = new_reports.id;
Run Code Online (Sandbox Code Playgroud)

我使用的测试数据:

SELECT reports FROM test;
                                               reports                                               
-----------------------------------------------------------------------------------------------------
 {"data": {"id": "a1aldjfg3f", "pages": [{"type": "pagea"}, {"type": "pagec"}], "activity": "test"}}
 {"data": {"id": "a1aldjfg3f", "pages": [{"type": "pagea"}, {"type": "pageb"}], "activity": "test"}}
 {"data": {"id": "a1aldjfg3f", "pages": [{"type": "pageb"}, {"type": "pagec"}], "activity": "test"}}
(3 rows)
Run Code Online (Sandbox Code Playgroud)

...执行查询后:

SELECT reports FROM test;
                                               reports                                               
-----------------------------------------------------------------------------------------------------
 {"data": {"id": "a1aldjfg3f", "pages": [{"type": "pagea"}, {"type": "pagec"}], "activity": "test"}}
 {"data": {"id": "a1aldjfg3f", "pages": [{"type": "pagea"}], "activity": "test"}}
 {"data": {"id": "a1aldjfg3f", "pages": [{"type": "pagec"}], "activity": "test"}}
(3 rows)
Run Code Online (Sandbox Code Playgroud)

我希望这对你有用。


kli*_*lin 5

该运算符不能应用于此处,因为根据文档,-右侧操作数是定义键的字符串:

从左操作数中删除键/值对或字符串元素。键/值对根据其键值进行匹配。

从 json 数组中删除 json 对象可以通过解压数组并查找对象的索引来完成。使用此方法的查询可能过于复杂,因此在这种情况下定义自定义函数非常方便。

create or replace function jsonb_remove_array_element(arr jsonb, element jsonb)
returns jsonb language sql immutable as $$
    select arr- (
        select ordinality- 1
        from jsonb_array_elements(arr) with ordinality
        where value = element)::int
$$;
Run Code Online (Sandbox Code Playgroud)

以及更新:

update my_table
set reports = 
    jsonb_set(
        reports, 
        '{data,pages}', 
        jsonb_remove_array_element(reports->'data'->'pages', '{"type":"pageb"}')
        )
where reports->'data'->'pages'  @> '[{"type":"pageb"}]';
Run Code Online (Sandbox Code Playgroud)

rextester 中的工作示例。