postgresql中更新jsonb中某一个子属性的值需要将其转换为jsonb格式,如果不转换只能是string类型,不能是其他类型
detail_info 中的子元素子属性增加固定值
detail_info 元素为 :{"student": {"educationStats": {"other": 0, "doctor": 0, "master": 0, "college": 0, "bachelor": 0}}}
需要 to_jsonb()做转换
update
t_job_info
set
detail_info = jsonb_set(detail_info, '{student,educationStats,other}', to_jsonb((((detail_info::json#>>'{student}')::json#>>'{educationStats}')::json#>>'{other}')::text::int + 1), false)
where
id = 80;
要注意的一点是,需要将jsonb_set()中第三个属性,也就是newValue 用 to_jsonb()做一次转换,如果newValue是string类型则不用修改
例如:不用to_jsonb()做转换
update
t_job_info
set
detail_info = jsonb_set(detail_info, '{student,educationStats,other}', '23', false)
where
id = 80;
花了2小时查阅文档发现并没有明确说明,自己摸索得出,感谢支持
翻阅资料:http://postgres.cn/docs/9.6/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE