码迷,mamicode.com
首页 > 数据库 > 详细

Postgresql Jsonb字段内含数组属性的删除元素操作

时间:2016-09-18 19:41:57      阅读:502      评论:0      收藏:0      [点我收藏+]

标签:

 1.创建示例表

create temp table settings as
select 
    {"west": [
      {"id": "aa92f346-7a93-4443-949b-4eab0badd983", "version": 1},
      {"id": "cd92e346-6b04-3456-050a-5eeb0bddd027", "version": 3}
    ]}::jsonb as value;

2.如下保留version=1的数据, 如果把where (j->>version‘)::int = 1改为where (j->>version‘)::int <> 1 则进行删除操作

update settings set value = jsonb_set(value, {west}, jsonb_build_array(ARRAY(select j from
(SELECT jsonb_array_elements(value->west) j from settings ) as b
where (j->>version)::int = 1)))

参考资料:

http://stackoverflow.com/questions/38640168/find-position-of-object-in-postgres-jsonb-array

http://dba.stackexchange.com/questions/54283/how-to-turn-json-array-into-postgres-array

 

Postgresql Jsonb字段内含数组属性的删除元素操作

标签:

原文地址:http://www.cnblogs.com/MonkChen/p/5882615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!