作为一个笔记应用程序,我现在的PostgreSQL模式中有以下json文档:
postgres=# select id, data from notes where id=107;
id | data
-----+-----------------------------------------------------------------
107 | {"tag": "sample tag", "title": "sample title", "content": "sample title\n\nsample text"}
为了能够在将来使用多个标记,我希望将数据库中的每个现有条目从上面转换到一个数组中,如下所示:"tag": ["sample tag"]
我做了一些研究,通过尝试,我得到了一些类似的东西:
UPDATE notes SET data = jsonb_set(data, '{tag}', $$["sample tag", "xyz"]$$);
它实际上将行从"tag": "sample tag"
更新为"tag": ["sample tag", "xyz"]
,但我希望使用现有标记动态地运行所有条目。我想不出一种在jsonb_set
部件中运行子查询的方法。
发布于 2018-08-25 20:37:51
update notes
set data = jsonb_set(data, '{tag}', jsonb_build_array(data->'tag'));
https://stackoverflow.com/questions/52016987
复制相似问题