Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJdkb-0005ww-Ls for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2017 11:08:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJdkb-0006Te-3t for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2017 11:08:29 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eJdkC-0002RK-8F for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2017 11:08:04 +0000 Received: from smtp.nspdc.no ([185.161.76.37]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eJdk8-0001tp-EW for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2017 11:08:02 +0000 Date: Tue, 28 Nov 2017 12:07:58 +0100 From: Bjorn T Johansen To: pgsql-general@lists.postgresql.org Subject: Re: jsonb and where clause? Message-ID: <20171128120758.76d98ff4@pennywise-btj> In-Reply-To: <7461ce91-bbbe-0389-9aac-3c68a0d5d240@postgrespro.ru> References: <20171128091807.218d7809@pennywise-btj> <20171128112541.0b35d714@pennywise-btj> <7461ce91-bbbe-0389-9aac-3c68a0d5d240@postgrespro.ru> Organization: Havleik Consulting X-Mailer: Claws Mail 3.15.1-dirty (GTK+ 2.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, 28 Nov 2017 13:52:59 +0300 "Ivan E. Panchenko" wrote: > 28.11.2017 13:25, Bjorn T Johansen =D0=BF=D0=B8=D1=88=D0=B5=D1=82: > > On Tue, 28 Nov 2017 11:28:55 +0300 > > "Ivan E. Panchenko" wrote: > > =20 > >> Hi Bjorn, > >> > >> 28.11.2017 11:18, Bjorn T Johansen =D0=BF=D0=B8=D1=88=D0=B5=D1=82: =20 > >>> Hi. > >>> > >>> Just starting to look at how to use jsonb columns and I have a questi= on. 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' =3D 'John Doe' (whe= re info is the jsonb column) > >>> > >>> > >>> But what if the jsonb column contains an json array, how can I search= then? > >>> > >>> info -> [ { "customer" : "John Doe" } ] =20 > >> 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 > >> =20 > >>> > >>> btw, using PostgreSQL 9.6 but will be moving to 10 soon. > >>> > >>> > >>> Regards, > >>> > >>> BTJ > >>> =20 > >> Regards, > >> Ivan > >> =20 > > 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? =20 > Yes, definitely it works, and is be supported by GIN index. >=20 > Nevertheless, I recommend you to have a look at JSQUERY which allows=20 > more complex queries, also with index support. > > > > > > BTJ > > =20 > Regards, > Ivan >=20 Ok, will have a look at JSQUERY also... :) BTJ