PostgreSQL 重命名 jsonb 字段中的属性 [英] PostgreSQL rename attribute in jsonb field

查看:30
本文介绍了PostgreSQL 重命名 jsonb 字段中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 postgresql 9.5 中,有没有办法重命名 jsonb 字段中的属性?

In postgresql 9.5, is there a way to rename an attribute in a jsonb field?

例如:

{ "nme" : "test" }

应该改名为

{ "name" : "test"}

推荐答案

UPDATE 中使用 删除 (-) 和连接 (||) 运算符,例如:

In UPDATE use delete (-) and concatenate (||) operators, e.g.:

create table example(id int primary key, js jsonb);
insert into example values
    (1, '{"nme": "test"}'),
    (2, '{"nme": "second test"}');

update example
set js = js - 'nme' || jsonb_build_object('name', js->'nme')
where js ? 'nme'
returning *;

 id |           js            
----+-------------------------
  1 | {"name": "test"}
  2 | {"name": "second test"}
(2 rows)

这篇关于PostgreSQL 重命名 jsonb 字段中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆