Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJdVk-0004z1-4Y for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2017 10:53:09 +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 1eJdVj-0007CV-Nw for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2017 10:53:07 +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 1eJdVj-0007CL-9k for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2017 10:53:07 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eJdVf-0001aE-LV for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2017 10:53:06 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 9CF8621C1D26 for ; Tue, 28 Nov 2017 13:53:00 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from [192.168.27.104] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 2D34D21C0644 for ; Tue, 28 Nov 2017 13:52:59 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1511866380; bh=lik0hIY7M2Uxtyu1fBemybC0DBu2qMdwbu6aET7Jfb8=; h=Subject:To:References:From:Date:In-Reply-To; b=hIrcC9/p4tqQYmPBUiXIAY/3Bzv4wegrCPtkD7uWgGH5h2X21jsdB05l6xZifIseM 77DmNccZKnqKVGq2MOXRIR+j2274r62rfj5YmqYGC1IirXnaHL0r84oNtwIgCzBz1X BAbp3ZAs1fIaPdOwIAXQh2R93ifMkZZHWZRcawpA= Subject: Re: jsonb and where clause? To: pgsql-general@lists.postgresql.org References: <20171128091807.218d7809@pennywise-btj> <20171128112541.0b35d714@pennywise-btj> From: "Ivan E. Panchenko" Message-ID: <7461ce91-bbbe-0389-9aac-3c68a0d5d240@postgrespro.ru> Date: Tue, 28 Nov 2017 13:52:59 +0300 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20171128112541.0b35d714@pennywise-btj> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 28.11.2017 13:25, Bjorn T Johansen пишет: > On Tue, 28 Nov 2017 11:28:55 +0300 > "Ivan E. Panchenko" 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