首页 > 解决方案 > Postgres jsonb_set 连接当前值

问题描述

我正在尝试使用 jsonb_set 来更新我的数据库中的一系列 json 对象。我可以获得一个使用字符串值更新对象的查询,但是我似乎无法使用当前值更新它。

UPDATE entity
SET properties = jsonb_set(properties, '{c_number}', concat('0', properties->>'c_number'))
WHERE type = 1 and length(properties->>'c_number') = 7

以上不适用于当前格式,我认为问题出properties->>'c_number'在 jsonb_set 内部。有没有办法可以访问当前值并简单地添加前导 0?

标签: postgresqljsonb

解决方案


找到了解决方案:

UPDATE entity
SET properties = jsonb_set(properties, '{c_number}', concat('"0', properties->>'c_number', '"')::jsonb)
WHERE type = 1 and length(properties->>'c_number') = 7

推荐阅读