如何在Postgresql中选择和更新JSON数组元素?
我正在使用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","number":"555-555-0030"},"number":"555-555-0020"},"number": "555-555-0010"}]}'); 现在我想弄清楚如何A,选择名为John Doe的行,并将他的手机号码更新为“555-555-0003”. 从这里Postgresql 9.6 documentation我发现我可以查询这样的正确文件: select c_json from t_json where c_json->>'name' = 'John Doe'; 但我没有看到如何按类型选择手机阵列中的正确子文档,并更新数值.谁能帮我吗? 编辑 我需要假设子文档有额外的值并且不一致.所以我在上面添加了一些.我非常确定此时无法在没有数据丢失的情况下进行此更新. 解决方法
如果您想获取电话号码,请使用此号码.相应文件是https://www.postgresql.org/docs/9.6/static/queries-table-expressions.html#QUERIES-LATERAL SELECT c_json ->> 'name',phones.type,phones.number FROM t_json CROSS JOIN json_to_recordset(c_json -> 'phones') AS phones("type" TEXT,"number" TEXT); 如果您想通过电话号码搜索,这可以: SELECT * FROM t_json WHERE (c_json -> 'phones')::JSONB @> '[{"type":"mobile","number":"555-555-0000"}]'::JSONB;
正如评论中所提到的,有一个类似的问题,How do I modify fields inside the new PostgreSQL JSON datatype? 还有其他方法可以做到这一点,比如 UPDATE t_json SET c_json = newvalue FROM ( SELECT to_json(updated) AS newvalue FROM ( SELECT c_json ->> 'name' as "name",json_agg(json_build_object('type','number',CASE phones.type WHEN 'mobile' THEN '555-555-0003' ELSE phones.number END) ) AS phones FROM t_json CROSS JOIN json_to_recordset(c_json -> 'phones') AS phones("type" TEXT,"number" TEXT) WHERE c_json->>'name' = 'John Doe' GROUP BY name ) as updated ) AS sub WHERE c_json ->> 'name' = 'John Doe'; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |