Update multiple values in a jsonb data in PostgreSQL

北城以北 提交于 2019-12-07 13:11:36

问题


I need to update a jsonb data(column->users) in my table 'settings' My jsonb data is like

'{
    "Email": "aaaa",
    "UserId": "49",
    "Created": "11/13/2016",
    "EntityId": "1",
    "IsActive": "False",
    "Modified": "11/13/2016",
    "Username": "aa"
}' 

In this json string I need to update Email,IsActive,Username together. I tried the below update query,its working fine. But that is for a single value updation.

UPDATE settings 
SET users = jsonb_set(users, '{Email}', '"aa"') 
WHERE users @> '{"UserId":"49"}';

How to update for multiple value updation? I am using postgres 9.5.


回答1:


Use the concatenation operator:

UPDATE settings 
SET users = users || '{"Email": "new email", "IsActive": "True", "Username": "new username"}'
WHERE users @> '{"UserId":"49"}';


来源:https://stackoverflow.com/questions/40583639/update-multiple-values-in-a-jsonb-data-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!