Re: replacing jsonb field value

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: "john(dot)tiger" <john(dot)tigernassau(at)gmail(dot)com>
Cc: PostgreSQL mailing lists <pgsql-general(at)postgresql(dot)org>
Subject: Re: replacing jsonb field value
Date: 2015-05-30 08:00:21
Message-ID: CAB7nPqSLT6iNa+3f2WzcHMO759CFDOxPxYWy8G6718=F-5ROKw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, May 30, 2015 at 11:52 AM, john.tiger <john(dot)tigernassau(at)gmail(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bob Futrelle 2015-05-30 11:48:48 How to retrieve Comment text using SQL, not psql?
Previous Message Thomas Munro 2015-05-30 03:53:47 Re: [HACKERS] Re: 9.4.1 -> 9.4.2 problem: could not access status of transaction 1