我使用的是9.6版本。我有一些类似于这样的文档:
{
"name" : "John Doe",
"phones" : [
{
"type" : "mobile",
"number" : "555-555-0000",
"deleted": false
},
{
"type" : "home",
"number" : "555-555-0001",
"needsUpdated" : true
},
{
"type" : "work",
"number" : "555-555-0002"
}
]
}
我按照以下方式创建它们:
create table t_json (c_json json not null);
insert into t_json (c_json) values ('{"name":"John Doe","phones": [{"type":"mobile","number":"555-555-0000"},{"type":"home","number":"555-555-0001"},{"type": "work","number": "555-555-0002"}]}');
insert into t_json (c_json) values ('{"name":"Jane Dane","phones": [{"type":"mobile","number":"555-555-0030"},{"type":"home","number":"555-555-0020"},{"type": "work","number": "555-555-0010"}]}');
现在我正在尝试弄清如何
A
,选择名为John Doe的行,并将其手机号码更新为“555-555-0003”。
从这里Postgresql 9.6文档中,我发现可以像这样查询正确的文档:
select c_json from t_json where c_json->>'name' = 'John Doe';
我不知道如何通过类型选择电话数组中正确的子文档并更新号码值。有人能帮我吗?
编辑
我需要假设子文档有额外的值并且不一致。因此,我在上面添加了一些内容。我相信这个更新目前无法实现而不会丢失数据。