postgresql 9.5 版本中JSONB数据类型新增的一些函数与功能

JSONB-modifying operators and functions

In 9.3 (and to a greater extent in 9.4), JSONB data could be extracted using various functions and operators, but nothing that could actually modify the data. As of 9.5, JSONB data can now be modified.
jsonb || jsonb (concatenate / overwrite)

The || operator allows us to combine 2 jsonb objects. If there‘s overlap, values are replaced on the highest level.

For example, if we want to add values to a jsonb object:

# SELECT ‘{"name": "Joe", "age": 30}‘::jsonb || ‘{"town": "London"}‘::jsonb;
                    ?column?                   
----------------------------------------------
  {"age": 30, "name": "Joe", "town": "London"}
(1 row)

Or we can overwrite existing values:

# SELECT ‘{"town": "Dataville", "population": 4096}‘::jsonb || ‘{"population": 8192}‘::jsonb;
                  ?column?                  
-------------------------------------------
  {"town": "Dataville", "population": 8192}
(1 row)

Note that this only works on the highest level, so nested objects are replaced from the top level. For example:

# SELECT ‘{"name": "Jane", "contact": {"phone": "01234 567890", "mobile": "07890 123456"}}‘::jsonb || ‘{"contact": {"fax": "01987 654321"}}‘::jsonb;
                        ?column?                       
------------------------------------------------------
  {"name": "Jane", "contact": {"fax": "01987 654321"}}
(1 row)

jsonb - text / int (remove key / array element)

We can remove keys from a jsonb object with the - operator:

# SELECT ‘{"name": "James", "email": "[email protected]"}‘::jsonb - ‘email‘;
       ?column?      
  -------------------
   {"name": "James"}
  (1 row)

Or remove values from an array (base 0):

# SELECT ‘["red","green","blue"]‘::jsonb - 1;
     ?column?     
-----------------
  ["red", "blue"]
(1 row)

jsonb - text[] / int (remove key / array element in path)

The previous example, we can remove keys or array elements, but not any lower than the highest level, so we can provide a path to the value we want to delete using a text array. Here we‘ll want to remove the fax number from within the contact value:

# SELECT ‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb - ‘{contact,fax}‘::text[];
                         ?column?                         
---------------------------------------------------------
  {"name": "James", "contact": {"phone": "01234 567890"}}
(1 row)

Or we can remove an array value. Here we‘ll get rid of the array value as index 1 (2nd value):

# SELECT ‘{"name": "James", "aliases": ["Jamie","The Jamester","J Man"]}‘::jsonb - ‘{aliases,1}‘::text[];
                      ?column?                     
--------------------------------------------------
  {"name": "James", "aliases": ["Jamie", "J Man"]}
(1 row)

jsonb_replace function

The above lets us delete values in a path, but not update them, so we have the jsonb_replace function for that. We‘ll update the phone value within the contact value:

# SELECT jsonb_replace(‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb, ‘{contact,phone}‘, ‘"07900 112233"‘::jsonb);
                                  jsonb_replace                                  
--------------------------------------------------------------------------------
  {"name": "James", "contact": {"fax": "01987 543210", "phone": "07900 112233"}}
(1 row)

jsonb_pretty

Notice that jsonb doesn‘t preserve white-space, so no matter how effort you went to in order to make the object easier to read, it will end up as a long string. Well jsonb_pretty will format it for you. If we use the previous jsonb example and wrap it all in a jsonb_pretty function:

# SELECT jsonb_pretty(jsonb_replace(‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb, ‘{contact,phone}‘, ‘"07900 112233"‘::jsonb));
           jsonb_pretty           
---------------------------------
  {                              +
      "name": "James",           +
      "contact": {               +
          "fax": "01987 543210", +
          "phone": "07900 112233"+
      }                          +
  }
(1 row)

Much easier to read.

时间: 2024-08-06 11:17:53

postgresql 9.5 版本中JSONB数据类型新增的一些函数与功能的相关文章

在win10 LTSB版本中使用UWF组件,实现影子保护功能,提供稳定,高速的开发环境

Win10 LTSB,比win8好用,又没有APP功能,老机运行非常稳定.流畅.安装运行一段时间后,安装了UWF组件,提高安全性和流畅性. Windows UWF(统一写保护)组件,功能: 1.当内存盘用,加强性能,提高速度: 2.省SSD写入: 3.当还原卡用,防熊孩子 开启方法: 1.程序和功能→启用或关闭Windows功能,勾选"统一写入筛选器" 2.UWF设置 手动启动及设定 (要重新启动系统才生效,关机再开是没用的) : 用管理员模式打开控制台,挑选下列可使用命令行 uwfm

PostgreSQL、SQL Server数据库中的数据类型的映射关系

PostgreSQL 8.1 轰动发布,我也打算将原来使用 SQL Server 的一些应用迁移到 PostgreSQL 上,首先需要迁移的是表,那么这就必须要先搞清楚这两个数据库中的数据类型的映射关系.查了下 PostgreSQL 文档特制作如下对应表格,以飨各位. 注意:PostgreSQL 中的 money(货币)数据类型现在已经废弃,用 numeric 或 decimal 以及和 to_char 函数一起使用就可以取代它. SQL Server 中的 datetime.smalldate

【转】C#各个版本中的新增特性详解

转自:http://www.cnblogs.com/knowledgesea/p/6694979.html 序言 自从2000年初期发布以来,c#编程语言不断的得到改进,使我们能够更加清晰的编写代码,也更加容易维护我们的代码,增强的功能已经从1.0搞到啦7.0甚至7.1,每一次改过都伴随着.NET Framework库的相应支持,也不断的带给我们期待与惊喜.下面我们就对c#一路走到现在,做一个回顾与学习. C#语言目标与前行 c#的设计目标是有以下几点: 旨在是一种简单,现代,通用的面向对象编程

MySQL中各种数据类型的长度及在开发中如何选择

接触MySQL这个数据库大概快要两年了,不过由于没有特别深入系统的去学习,大多也是停留在一知半解的状态.今天在工作中刚好碰到了表设计的问题,顺便写篇博客,把MySQL中数据类型和字段类型选择这方面给弄清楚. MySQL中的数据类型大体分为三大类,数值类型,时间日期类型以及字符串类型.下面将对这三种类型进行详细的介绍. 一.数值类型 MySQL 支持所有标准SQL 中的数值类型,其中包括严格数值类型(INTEGER.SMALLINT.DECIMAL 和NUMERIC),以及近似数值数据类型(FLO

c#中的数据类型简介(数组)

c#中的数据类型简介(数组) 数组定义 可以将数组看成相同数据类型的一组或多组数据,包括一维数组,多维数组和交错数组 数据申明 一维数组的几种申明和实例化 type[]  typeName = new type[n]:                                                //定义数组但是未赋值 type[0] = item1; type[1] = item2; type[2] = item3; ...... type[n-1] =itemn; type[

sqlite3中BLOB数据类型存储大对象运用示例

1:常用接口 个人比较喜欢sqlite, 使用最方便,唯一的准备工作是下载250K的源:而且作者很热心,有问必答. 以下演示一下使用sqlite的步骤,先创建一个数据库,然后查询其中的内容.2个重要结构体和5个主要函数: sqlite3               *pdb, 数据库句柄,跟文件句柄FILE很类似 sqlite3_stmt      *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句 sqlite3_open(),   打开数据库 sqlite3_e

微软BI 之SSIS 系列 - 2008 版本中变量 Int64 无法保存 bigint 类型数据的BUG

开篇介绍 这是今天在帮别人看一个 2008R2 版本的项目时发现的一个 Bug,这个 Bug 在 SQL SERVER 2012 有的版本中可能已经解决,但在论坛上看到有的仍然存在. 在 SQL SERVER 2008 R2 版本中,比如从一个数据库表中取一个 BIGINT 类型的数据,赋值给一个 Int64 类型的变量,按照我们的理解肯定是正确的. 但是结果是报错了 - 错误原因:类型不一致造成的. [Execute SQL Task] Error: An error occurred whi

[译] OpenStack Liberty 版本中的53个新变化

一个新的秋季,一个新的OpenStack 版本.OpenStack 的第12个版本,Liberty,在10月15日如期交付,而且目前发行版本已经备好了.那么我们期望能从过去六个月时间的开发中获得些什么呢?  正如过去几年一样,每个版本都有大量的变化和引入新的功能,但是现在,OpenStack 已经达到一个新的位置,那就是大部分的必要功能都已经齐备了,因此,现在更多的是增量式的变化-只有一些偶然性爆发的领域是例外,比如容器.  当然,这不是说容器就是过去六个月内的所有值得期待的东西.除了容器,还有

MySQL 中的数据类型介绍

MySQL 中的数据类型介绍 标签: mysql数据类型mysql全部数据类型mysql字段类型mysql数据存储mysql 2016-04-29 20:24 53643人阅读 评论(11) 收藏 举报  分类: 服务器及软件---MySQL数据库(4)  版权声明:http://blog.csdn.net/anxpp 目录(?)[+] 据我统计,MySQL支持39种(按可使用的类型字段统计,即同义词也作多个)数据类型.下面的介绍可能在非常古老的MySQL版本中不适用. 转载请注明出处:http