sql - 在 postgresql 中编辑 jsonb 数组的字段

标签 sql json postgresql sql-update postgresql-9.5

我在数据库中有以下 jsonb:

[
    {
        "state": 2,
        "activity": "EJECUCIÓN",
        "final_date": "2020-02-24",
        "activity_id": 1,
        "current_days": -7,
        "initial_date": "2020-02-24",

    },
    {
        "state": 2,
        "activity": "REVISIÓN",
        "final_date": "2020-02-25",
        "activity_id": 2,
        "current_days": 0,
        "initial_date": "2020-02-25",

    },
    {
        "state": 2,    
        "activity": "RECEPCIÓN",
        "final_date": "2020-02-27",
        "activity_id": 4,
        "current_days": 0,
        "initial_date": "2020-02-27"


    } ]

我运行以下查询来更新 current_days 字段:

WITH activity_state     as  ( SELECT taex_id,('{'||index-1||',current_days}')::text[] as pathe ,
        ((task_activity->>'final_date')::date - current_date) as current_days,
        task_activity->'state' as state,
        task_activity->>'final_date' as final_date,
        task_activity->>'current_days' as curren   FROM task_executions,jsonb_array_elements(taex_activitygraph) with ordinality arr(task_activity,index)   WHERE task_activity->>'state' = '2'  )  

update task_executions SET  taex_activitygraph = jsonb_set(taex_activitygraph,activity_state.pathe,to_jsonb(current_days),true) FROM activity_state  WHERE task_executions.taex_id = activity_state.taex_id  AND activity_state.state = '2'

但是该查询仅更新了 JSON 数组中存在的第一个元素,尽管在查询的第一部分中,其他元素并未发生更改。

( SELECT taex_id,('{'||index-1||',current_days}')::text[] as pathe ,
        ((task_activity->>'final_date')::date - current_date) as current_days,
        task_activity->'state' as state,
        task_activity->>'final_date' as final_date,
        task_activity->>'current_days' as curren   FROM task_executions,jsonb_array_elements(taex_activitygraph) with ordinality arr(task_activity,index)   WHERE task_activity->>'state' = '2'  )

它给我带来了数组中应该更新的所有元素,但第二部分是应该更新它们的地方:

update task_executions SET  taex_activitygraph = jsonb_set(taex_activitygraph,activity_state.pathe,to_jsonb(current_days),true) FROM activity_state  WHERE task_executions.taex_id = activity_state.taex_id  AND activity_state.state = '2'

只需更新我的第一个项目即可。

最佳答案

假设这个结构和数据:

postgres=# \d task_executions 
               Table "public.task_executions"
       Column       | Type  | Collation | Nullable | Default 
--------------------+-------+-----------+----------+---------
 task_activitygraph | jsonb |           |          | 

postgres=# SELECT jsonb_pretty(task_activitygraph) FROM task_executions ;
             jsonb_pretty             
--------------------------------------
 [                                   +
     {                               +
         "state": 2,                 +
         "activity": "EJECUCIÓN",    +
         "final_date": "2020-02-24", +
         "activity_id": 1,           +
         "current_days": -7,         +
         "initial_date": "2020-02-24"+
     },                              +
     {                               +
         "state": 2,                 +
         "activity": "REVISIÓN",     +
         "final_date": "2020-02-25", +
         "activity_id": 2,           +
         "current_days": 0,          +
         "initial_date": "2020-02-25"+
     }                               +
 ]
(1 row)

...此更新应该有效:

postgres=# UPDATE task_executions
SET task_activitygraph = (
  SELECT jsonb_agg(
    CASE WHEN elem->>'state' = '2'
    THEN 
      jsonb_set(
        elem,
        '{current_days}',
        to_jsonb((elem->>'final_date')::date - current_date)
      )
    ELSE
      elem
    END
  )
  FROM jsonb_array_elements(task_activitygraph) AS a(elem)
);
UPDATE 1

文档:https://www.postgresql.org/docs/9.5/functions-json.html

旁注:在事务数据库中(您有许多并发客户端,处理速度和存储效率很重要),如果您的对象具有固定结构,不要将数据存储为 JSON。请改用关系数据模型。

关于sql - 在 postgresql 中编辑 jsonb 数组的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60531173/

相关文章:

mysql - 从 MySQL 迁移后查询中的 PostgreSQL 分组错误

sql - sys.dm_exec_sql_text 如何工作?

javascript - 从 JSON 动画化 Google Chart

json - ARM模板: How can i ensure Web App slots do not get the Web App Settings deployed over?

postgresql - 使用 Ansible 运行 PostgreSQL 脚本

postgresql - 哪个支持 JSONB 的 ORM 用 Sails 更好

java - 修复无效标识符的错误

mysql - 使用游标的过程循环中出错

python - 字典的字典的 JSON 编码

sql - 查找未在旧订阅阈值内重新订阅的订阅者