pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
jsonb and where clause?
5+ messages / 2 participants
[nested] [flat]

* jsonb and where clause?
@ 2017-11-28 08:18  Bjorn T Johansen <btj@havleik.no>
  0 siblings, 1 reply; 5+ messages in thread

From: Bjorn T Johansen @ 2017-11-28 08:18 UTC (permalink / raw)
  To: pgsql-general

Hi.

Just starting to look at how to use jsonb columns and I have a question. I have found out that I can use the following to search for a value inside
the jsonb column:

select * from orders where info ->> 'customer' = 'John Doe'    (where info is the jsonb column)


But what if the jsonb column contains an json array, how can I search then?

info -> [ { "customer" : "John Doe" } ]


btw, using PostgreSQL 9.6 but will be moving to 10 soon.


Regards,

BTJ




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: jsonb and where clause?
@ 2017-11-28 08:28  Ivan E. Panchenko <i.panchenko@postgrespro.ru>
  parent: Bjorn T Johansen <btj@havleik.no>
  0 siblings, 1 reply; 5+ messages in thread

From: Ivan E. Panchenko @ 2017-11-28 08:28 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi Bjorn,

28.11.2017 11:18, Bjorn T Johansen пишет:
> Hi.
>
> Just starting to look at how to use jsonb columns and I have a question. I have found out that I can use the following to search for a value inside
> the jsonb column:
>
> select * from orders where info ->> 'customer' = 'John Doe'    (where info is the jsonb column)
>
>
> But what if the jsonb column contains an json array, how can I search then?
>
> info -> [ { "customer" : "John Doe" } ]

If you know the index in array, you can search like
info->0->>'customer'
If you want to search in any array element, you need to use JSQUERY 
extension,
see https://github.com/postgrespro/jsquery

>
>
> btw, using PostgreSQL 9.6 but will be moving to 10 soon.
>
>
> Regards,
>
> BTJ
>
Regards,
Ivan




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: jsonb and where clause?
@ 2017-11-28 10:25  Bjorn T Johansen <btj@havleik.no>
  parent: Ivan E. Panchenko <i.panchenko@postgrespro.ru>
  0 siblings, 1 reply; 5+ messages in thread

From: Bjorn T Johansen @ 2017-11-28 10:25 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Tue, 28 Nov 2017 11:28:55 +0300
"Ivan E. Panchenko" <i.panchenko@postgrespro.ru> wrote:

> Hi Bjorn,
> 
> 28.11.2017 11:18, Bjorn T Johansen пишет:
> > Hi.
> >
> > Just starting to look at how to use jsonb columns and I have a question. I have found out that I can use the following to search for a value
> > inside the jsonb column:
> >
> > select * from orders where info ->> 'customer' = 'John Doe'    (where info is the jsonb column)
> >
> >
> > But what if the jsonb column contains an json array, how can I search then?
> >
> > info -> [ { "customer" : "John Doe" } ]  
> 
> If you know the index in array, you can search like
> info->0->>'customer'
> If you want to search in any array element, you need to use JSQUERY 
> extension,
> see https://github.com/postgrespro/jsquery
> 
> >
> >
> > btw, using PostgreSQL 9.6 but will be moving to 10 soon.
> >
> >
> > Regards,
> >
> > BTJ
> >  
> Regards,
> Ivan
> 

Thx... :)

btw, just managed to use the following sql:

select * from orders where info @> '{"recs": [{ "customer":"John Doe"}]}' 

(changed my json string to info -> { "recs: [ { "customer" : "John Doe" } ] }

And this seems to work but is this the "wrong" way of doing it or?


BTJ




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: jsonb and where clause?
@ 2017-11-28 10:52  Ivan E. Panchenko <i.panchenko@postgrespro.ru>
  parent: Bjorn T Johansen <btj@havleik.no>
  0 siblings, 1 reply; 5+ messages in thread

From: Ivan E. Panchenko @ 2017-11-28 10:52 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


28.11.2017 13:25, Bjorn T Johansen пишет:
> On Tue, 28 Nov 2017 11:28:55 +0300
> "Ivan E. Panchenko" <i.panchenko@postgrespro.ru> wrote:
>
>> Hi Bjorn,
>>
>> 28.11.2017 11:18, Bjorn T Johansen пишет:
>>> Hi.
>>>
>>> Just starting to look at how to use jsonb columns and I have a question. I have found out that I can use the following to search for a value
>>> inside the jsonb column:
>>>
>>> select * from orders where info ->> 'customer' = 'John Doe'    (where info is the jsonb column)
>>>
>>>
>>> But what if the jsonb column contains an json array, how can I search then?
>>>
>>> info -> [ { "customer" : "John Doe" } ]
>> If you know the index in array, you can search like
>> info->0->>'customer'
>> If you want to search in any array element, you need to use JSQUERY
>> extension,
>> see https://github.com/postgrespro/jsquery
>>
>>>
>>> btw, using PostgreSQL 9.6 but will be moving to 10 soon.
>>>
>>>
>>> Regards,
>>>
>>> BTJ
>>>   
>> Regards,
>> Ivan
>>
> Thx... :)
>
> btw, just managed to use the following sql:
>
> select * from orders where info @> '{"recs": [{ "customer":"John Doe"}]}'
>
> (changed my json string to info -> { "recs: [ { "customer" : "John Doe" } ] }
>
> And this seems to work but is this the "wrong" way of doing it or?
Yes, definitely it works, and is be supported by GIN index.

Nevertheless, I recommend you to have a look at JSQUERY which allows 
more complex queries, also with index support.
>
>
> BTJ
>
Regards,
Ivan




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: jsonb and where clause?
@ 2017-11-28 11:07  Bjorn T Johansen <btj@havleik.no>
  parent: Ivan E. Panchenko <i.panchenko@postgrespro.ru>
  0 siblings, 0 replies; 5+ messages in thread

From: Bjorn T Johansen @ 2017-11-28 11:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Tue, 28 Nov 2017 13:52:59 +0300
"Ivan E. Panchenko" <i.panchenko@postgrespro.ru> wrote:

> 28.11.2017 13:25, Bjorn T Johansen пишет:
> > On Tue, 28 Nov 2017 11:28:55 +0300
> > "Ivan E. Panchenko" <i.panchenko@postgrespro.ru> wrote:
> >  
> >> Hi Bjorn,
> >>
> >> 28.11.2017 11:18, Bjorn T Johansen пишет:  
> >>> Hi.
> >>>
> >>> Just starting to look at how to use jsonb columns and I have a question. I have found out that I can use the following to search for a value
> >>> inside the jsonb column:
> >>>
> >>> select * from orders where info ->> 'customer' = 'John Doe'    (where info is the jsonb column)
> >>>
> >>>
> >>> But what if the jsonb column contains an json array, how can I search then?
> >>>
> >>> info -> [ { "customer" : "John Doe" } ]  
> >> If you know the index in array, you can search like
> >> info->0->>'customer'
> >> If you want to search in any array element, you need to use JSQUERY
> >> extension,
> >> see https://github.com/postgrespro/jsquery
> >>  
> >>>
> >>> btw, using PostgreSQL 9.6 but will be moving to 10 soon.
> >>>
> >>>
> >>> Regards,
> >>>
> >>> BTJ
> >>>     
> >> Regards,
> >> Ivan
> >>  
> > Thx... :)
> >
> > btw, just managed to use the following sql:
> >
> > select * from orders where info @> '{"recs": [{ "customer":"John Doe"}]}'
> >
> > (changed my json string to info -> { "recs: [ { "customer" : "John Doe" } ] }
> >
> > And this seems to work but is this the "wrong" way of doing it or?  
> Yes, definitely it works, and is be supported by GIN index.
> 
> Nevertheless, I recommend you to have a look at JSQUERY which allows 
> more complex queries, also with index support.
> >
> >
> > BTJ
> >  
> Regards,
> Ivan
> 

Ok, will have a look at JSQUERY also... :)

BTJ




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2017-11-28 11:07 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-11-28 08:18 jsonb and where clause? Bjorn T Johansen <btj@havleik.no>
2017-11-28 08:28 ` Ivan E. Panchenko <i.panchenko@postgrespro.ru>
2017-11-28 10:25   ` Bjorn T Johansen <btj@havleik.no>
2017-11-28 10:52     ` Ivan E. Panchenko <i.panchenko@postgrespro.ru>
2017-11-28 11:07       ` Bjorn T Johansen <btj@havleik.no>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox