Re: In need of some JSONB examples ?

From: Christophe Pettus <xof(at)thebuild(dot)com>
To: Tim Smith <randomdev4+postgres(at)gmail(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: In need of some JSONB examples ?
Date: 2015-01-23 17:32:54
Message-ID: 41B40C0D-A312-423B-BD85-E5AA6E7FF28C@thebuild.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Jan 23, 2015, at 7:40 AM, Tim Smith <randomdev4+postgres(at)gmail(dot)com> wrote:

> re: (a)
>
>> see the documentation pertaining to 'jsonb indexing', to wit:
>>
>> -- Find documents in which the key "company" has value "Magnafone"
>> SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"company":
>> "Magnafone"}';
>
> Nope, sorry, tried that. Doesn't work for me. Hence the question. ;-)

The problem is that @> only operates at the top level of the JSON object presented to it:

xof=# TABLE j;
f
--------------------
[{"a": 1, "b": 2}]
{"a": 1, "b": 2}
(2 rows)

xof=# SELECT * FROM j WHERE f @> $$ { "a": 1 } $$::jsonb;;
f
------------------
{"a": 1, "b": 2}
(1 row)

I'm actually not seeing a great solution to your particular problem. If you know for sure that everything always has the format you describe, you can use jsonb_array_elements to extract the individual members of the array, and use @> on them, via a JOIN, but it's not clear that an index will help you there.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tim Smith 2015-01-23 18:15:59 Re: In need of some JSONB examples ?
Previous Message Petr Novak 2015-01-23 16:55:08 Re: CLOG read problem after pg_basebackup