postgresql - 在 postgresql 中使用 jsonb_set 更新 jsonb 数组一个没有键的元素
问题描述
我有audits
带有 jsonb 字段的表data
,它有内容:
"products": [
{"id": 405, "color": null, "price": 850, "title": "test", "value": 52, "real_value": 105 },
{"id": 347, "color": null, "price": 195, "title": "test2", "value": 69, "real_value": 0}
]
对于更新,需要知道元素的键,但我不知道如何找到该元素的键(0)。例子:
UPDATE audits set data = jsonb_set(data::jsonb,'{"products",0,"real_value"}','125')
where id = 10 and enterprise_id = 1;
我尝试使用这个:
SELECT ( select index from
generate_series(0, jsonb_array_length(data->'products') - 1) as index
where data->'products'->index->>'id' = '347') as index_of_element
FROM audits;
但这对pg来说是一个非常艰难的操作。
我也知道如何找到这个对象的产品 ID:
select id as possiton from audits
where data->'products' @> '[{"id":347}]';
但不是在数组中键入(((
也许我可以使用另一个函数来更新数组中的项目,但我不知道如何。
解决方案
推荐阅读
- image - 图像出现在 dialogflow 聊天机器人中,但未显示在 whatsapp 聊天机器人中并导致 twilio 错误
- postgresql - 如何将存储为双精度值的儒略日期转换为分辨率至少为一分钟的时间戳?
- javascript - 偏移顶部 document.querySelector - Javascript
- python - 在python3中将JSON转换为CSV
- python - 安装 Tensorflow 时出现 CondaVerificationError
- c# - 在使用 XAML 岛的 WPF 应用程序中托管 UWP 控件的真正受支持的 Windows 10 最低版本是什么?
- python-3.x - 什么是列表的正确调用
- python-3.x - 使用 AWS-Lamda 调用 sagemaker 端点引发参数验证错误
- python-3.x - ValueError:概率不是非负的
- ada - SPARK 中的程序验证 - 计算数组中的元素