如何更新JSONB Postgres列中的整个JSON对象,除1个字段
例如,我有一个表:
CREATE TABLE fruit(id bigint, data jsonb);
例如,一行是:
1,
{
"type": "pinapple",
"store1": {
"first_added": "<some timestamp>",
"price": "10",
"store_id": "1",
"comments": "some comments..."
},
"store2": {
"first_added": "<some timestamp>",
"price": "11",
"store_id": "2",
"comments": "some comments..."
},
.... more stores
}
在更新的情况下,我有水果ID和存储数据:
1,
"store1": {
"price": "12",
"store_id": "1",
"comments": "some comments...V2"
}
除了第一个_ADDED字段外,我想在水果条目中更新整个商店对象(for Store1)。
是否知道如何通过JSONB运营商或功能来实现它?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
( $ 1 设置为值
{“ store1”:{“ PRICE”:“ 12”,“ store_id”:“ 1”,“注释”:“一些注释.. .v2“}}
。或者,如果需要动态的密钥,请使用
(
You can use
(online demo)
where the parameter
$1
is set to the value{"store1": {"price": "12", "store_id": "1", "comments": "some comments...V2"}}
.Or if you need the key to be dynamic, use
(online demo)
您可以使用
jsonb_set
函数来更改所需元素,然后使用jsonb_build_object
函数创建新数据集,然后将数据与|| 操作员以保留其余数据(first_added,...)
演示 dbfiddle
You can use the
jsonb_set
function to change the desired element, then use thejsonb_build_object
function to create a new dataset, then concatenate the data with the||
operator to keep the rest of the data(first_added,...)Demo in DBfiddle