我用来jsonb_set
在 postgres 中部分更新我的 jsonb 对象。
这就是文档关于这个功能的说法。
jsonb_set(
target jsonb, # The jsonb value you're amending.
path text[], # The path to the value you wish to add to or change, represented as a text array.
new_value jsonb, # The new object, key : value pair or array value(s) to add to or change.
create_missing boolean # An optional field that, if true (default), creates the value if the key doesn't already exist.
# If false, the path must exist for the update to happen, or the value won't be updated.
)
我认为create_missing
(默认情况下是这样)会导致不存在的路径出现在我的 jsonb 对象中,但似乎这只适用于最后一步(例如,不是递归的)。
查询
UPDATE myScheme.myTable SET data = jsonb_set(data, $1, $2, true) where id = $3;
$1 = {foo,bar,baz}
如果和我的电流将失败data = {foo: {}}
问题是:如何jsonb
在 PostgreSQL v9.5+ 中通过递归创建不存在的子对象来更新我的对象?