我有一个带有jsonb列的DB表,该列有一个实体,带有嵌套的子实体。假设我们有:
SELECT jsonb_set('{"top": {"nested": {"leaf" : 1}}}', '{top,nested,leaf}', '2');
通过将top.nested.leaf
更新为2来完成哪项工作。
但是,如果我们想要做多个字段,例如:
SELECT jsonb_set('{"top": {"nested": {"leaf" : 1}, "other_nested": {"paper": 0}}}', '[{top,nested,leaf}, {top,other_nested,paper}]', '[2, 2]');
以上说法不起作用并说:
ERROR: malformed array literal: "[{top,nested,leaf}, {top,other_nested,paper}]"
LINE 1: ...": {"leaf" : 1}, "other_nested": {"paper": 0}}}', '[{top,nes...
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
有什么想法吗?
答案 0 :(得分:1)
https://www.postgresql.org/docs/current/static/functions-json.html
jsonb_set(target jsonb, path text[], new_value jsonb[, create_missing boolean])
既不是路径,也不是新值不能有多个值。你必须为想要的结果运行两次,例如:
SELECT jsonb_set(
'{"top": {"nested": {"leaf" : 1}, "other_nested": {"paper": 0}}}'
, '{top,nested,leaf}'
, '2'
);
SELECT jsonb_set(
'{"top": {"nested": {"leaf" : 1}, "other_nested": {"paper": 0}}}'
, '{top,other_nested,paper}'
, '2'
);