如何修改 PostgreSQL JSONB 数据类型中的单个字段?
假设我有一张名为“动物”的表,如下所示:
id info
------------------------------------------------------------
49493 {"habit1":"fly","habit2":"dive","location":"SONOMA NARITE"}
我想简单地更改位置属性的值(例如,将文本更改为大写或小写)。所以更新后的结果是
id info
------------------------------------------------------------
49493 {"habit1":"fly","habit2":"dive","location":"sonoma narite"}
我在下面尝试过,但它不起作用
update animal set info=jsonb_set(info, '{location}', LOWER(info->>'location'), true) where id='49493';
----------------------------------
ERROR: function jsonb_set(jsonb, unknown, text, boolean) does not exist
LINE 7: update animal set info=jsonb_set(info, '{con...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function jsonb_set(jsonb, unknown, text, boolean) does not exist
如果我只是知道更新后的值是什么,那么我可以使用这个:
update animal set info=jsonb_set(info, '{location}', '"sonoma narite"', true) where id='49493';
但是,如果文本值未知,而我们只想做一些简单的操作,例如追加、前置、大写/小写,我无法简单地找到答案。
令我惊讶的是,jsonb set 函数没有提供这样一个简单的操作,即仅尝试更新 jsonb 内文本属性的大小写。
有人可以帮忙吗?