Re: replacing jsonb field value - Mailing list pgsql-general

From Michael Paquier
Subject Re: replacing jsonb field value
Date
Msg-id CAB7nPqSLT6iNa+3f2WzcHMO759CFDOxPxYWy8G6718=F-5ROKw@mail.gmail.com
Whole thread Raw
In response to replacing jsonb field value  ("john.tiger" <john.tigernassau@gmail.com>)
Responses Re: replacing jsonb field value
List pgsql-general
On Sat, May 30, 2015 at 11:52 AM, john.tiger <john.tigernassau@gmail.com> wrote:
> using 9.4.2
>
> suppose we have
> create table test (id serial primary key, data jsonb);
> insert into test (data) values ({"a":1, "b":2})
>
> want to replace "b" with 3
>
> okay, we are retrieving entire record
> res = select * from test where data ->> b = 2
>
> newrec = res
> newrec["b" = 3
>
> delete from test where data ->> b= 2
> insert into test (data) values (newrec)
>
> is this the best way until upsert arrives ?

Append the new value to it the existing field, jsonb has as property
to enforce key uniqueness, and uses the last value scanned for a given
key.
--
Michael


pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [HACKERS] Re: 9.4.1 -> 9.4.2 problem: could not access status of transaction 1
Next
From: Bob Futrelle
Date:
Subject: How to retrieve Comment text using SQL, not psql?