2
我使用jsonb_set
來部分更新postgres中的jsonb對象。遞歸更新PostgreSQL v9.5中的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
(默認情況下爲真)將導致unexisting路徑出現在我的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 + unexisting子對象的對象?
感謝您的回答,但它似乎不適合我。如果我目前的'data = {foo:{bar:{spam:1,more:2,eggs:3}}'怎麼辦?難道我不完全覆蓋'bar'而不是添加'baz'屬性?我在'data'對象中已經有了一些小小的功能,比如'bar'屬性如果它已經存在就會是子對象。 –