Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从嵌套的json-postgres中删除值_Json_Postgresql_Jsonb - Fatal编程技术网

从嵌套的json-postgres中删除值

从嵌套的json-postgres中删除值,json,postgresql,jsonb,Json,Postgresql,Jsonb,我在下面对json块进行了建模。我想根据示例中的AAA和BBB id,有选择地从我的_项目中删除各个块。也就是说,如果我试图删除我的_项下的AAA块,我只想删除{id:AAA},但是如果想删除BBB块,它会删除较大的{name:TestRZ,id:BBB,description:}块 我知道我可以使用-来删除整个块,比如选择“{sample_json}”::jsonb-“{my_items}”将清除整个my_items块。但我不知道如何使用它有条件地删除json父块下的子项。我还使用了类似于本例

我在下面对json块进行了建模。我想根据示例中的AAA和BBB id,有选择地从我的_项目中删除各个块。也就是说,如果我试图删除我的_项下的AAA块,我只想删除{id:AAA},但是如果想删除BBB块,它会删除较大的{name:TestRZ,id:BBB,description:}块

我知道我可以使用-来删除整个块,比如选择“{sample_json}”::jsonb-“{my_items}”将清除整个my_items块。但我不知道如何使用它有条件地删除json父块下的子项。我还使用了类似于本例的代码,通过在嵌套结构的节点中读取新数据并重写,将数据附加到嵌套结构中。更新数据集value=jsonb_setvalue,“{my_items}”,value->“items”|:“json_to_adds”,其中id='testnofeed'为true

但我不知道如何将这两种方法应用于:1使用-2在嵌套结构中删除数据,或2使用'jsonb_set'删除相同的数据。任何人对如何使用这些方法或其他方法进行此操作都有任何指导

{
  "urlName" : "testurl",
  "countryside" : "",
  "description" : "",
  "my_items" : [
     {
       "id" : "AAA"
     },
    {
      "name" : "TestRZ",
      "id" : "BBB",
      "description" : ""
    },
 ],
   "name" : "TheName"
 }
数据存储在值jsonb中。当我更新时,我将能够传递一种独特的类型,以便它只更新数据库中一行的json

  -- Table Definition
  CREATE TABLE "public"."data" (
 "id" varchar(100) NOT NULL,
 "kind" varchar(100) NOT NULL,
 "revision" int4 NOT NULL,
 "value" jsonb
  );

这适用于PostgreSQL 12及更高版本,并支持jsonpath。如果您没有jsonpath,请留下评论

with data as (
  select '{
  "urlName" : "testurl",
  "countryside" : "",
  "description" : "",
  "my_items" : [
     {
       "id" : "AAA"
     },
    {
      "name" : "TestRZ",
      "id" : "BBB",
      "description" : ""
    }
 ],
   "name" : "TheName"
 }'::jsonb as stuff
)
select jsonb_set(stuff, '{my_items}',
                   jsonb_path_query_array(stuff->'my_items', '$ ? (@."id" <> "AAA")'))
  from data;

                                                                     jsonb_set                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------
 {"name": "TheName", "urlName": "testurl", "my_items": [{"id": "BBB", "name": "TestRZ", "description": ""}], "countryside": "", "description": ""}
(1 row)
这方面的直接更新更为复杂:

with expand as (
  select d.id, d.value, e.item, e.rn
    from data d
   cross join lateral jsonb_array_elements(value->'my_items')
         with ordinality as e(item, rn)
), agg as (
  select id, jsonb_set(value, '{my_items}', jsonb_agg(item order by rn)) as new_value
    from expand
   where item->>'id' != 'AAA'
   group by id, value
)
update data
   set value = agg.new_value
  from agg
 where agg.id = data.id;

这适用于PostgreSQL 12及更高版本,并支持jsonpath。如果您没有jsonpath,请留下评论

with data as (
  select '{
  "urlName" : "testurl",
  "countryside" : "",
  "description" : "",
  "my_items" : [
     {
       "id" : "AAA"
     },
    {
      "name" : "TestRZ",
      "id" : "BBB",
      "description" : ""
    }
 ],
   "name" : "TheName"
 }'::jsonb as stuff
)
select jsonb_set(stuff, '{my_items}',
                   jsonb_path_query_array(stuff->'my_items', '$ ? (@."id" <> "AAA")'))
  from data;

                                                                     jsonb_set                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------
 {"name": "TheName", "urlName": "testurl", "my_items": [{"id": "BBB", "name": "TestRZ", "description": ""}], "countryside": "", "description": ""}
(1 row)
这方面的直接更新更为复杂:

with expand as (
  select d.id, d.value, e.item, e.rn
    from data d
   cross join lateral jsonb_array_elements(value->'my_items')
         with ordinality as e(item, rn)
), agg as (
  select id, jsonb_set(value, '{my_items}', jsonb_agg(item order by rn)) as new_value
    from expand
   where item->>'id' != 'AAA'
   group by id, value
)
update data
   set value = agg.new_value
  from agg
 where agg.id = data.id;

将在10分钟后回到计算机前试用,但您知道我将如何将此转换为{my_items}的更新。我会修改它来更新{my_items}部分的整个数据块吗?Thanks@personalt您必须更新整个jsonb。如果您在问题中加入表格定义,并指出是否有jsonpath,我可以在回答中举例说明。@nike organek-谢谢。我用表格定义更新了问题。对于jsonbpath,您是指在线评估器还是其他什么?我可以安装任何需要的东西,但我的计划是通过命令行脚本通过psql运行更新数据库matters@personalt我特别谈论的是jsonb_路径_查询_数组。这是PostgreSQL的最新添加。我在PostgreSQL 12示例中添加了一个示例。对于早期版本,您需要从@personal更新,只要是12或更高版本,您可以在我的答案中使用较短形式的更新。将在10分钟后回到计算机前尝试,但您知道我如何将此转换为{my_items}的更新。我会修改它来更新{my_items}部分的整个数据块吗?Thanks@personalt您必须更新整个jsonb。如果您在问题中加入表格定义,并指出是否有jsonpath,我可以在回答中举例说明。@nike organek-谢谢。我用表格定义更新了问题。对于jsonbpath,您是指在线评估器还是其他什么?我可以安装任何需要的东西,但我的计划是通过命令行脚本通过psql运行更新数据库matters@personalt我特别谈论的是jsonb_路径_查询_数组。这是PostgreSQL的最新添加。我在PostgreSQL 12示例中添加了一个示例。对于早期版本,您需要从@personalt执行更新,只要是12或更高版本,您可以在我的答案中使用更新的较短形式。