gpt4 book ai didi

postgresql - Postgres jsonb_set 多个嵌套字段

转载 作者:行者123 更新时间:2023-11-29 11:46:56 28 4
gpt4 key购买 nike

我有一个带有 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]');

以上不起作用并说:

错误:格式错误的数组文字:“[{top,nested,leaf}, {top,other_nested,paper}]”
第 1 行:...": {"leaf": 1}, "other_nested": {"paper": 0}}}', '[{top,nes...
^
详细信息:“[”必须引入明确指定的数组维度。

有什么想法吗?

最佳答案

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'
);

关于postgresql - Postgres jsonb_set 多个嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45481692/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com