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

 1.建立示例表json

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 则进行删除操做post

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)))

参考资料:ui

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

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

相关文章
相关标签/搜索