Postgresql Jsonb字段內含數組屬性的刪除元素操作


 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM