From: geoff hoffman <geoff@rxmg.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Working with JSONB data having node lists
Date: Tue, 30 Jan 2018 14:47:21 -0700
Message-ID: <93BF39AF-F96B-4437-BECE-CD7D7815CF4E@rxmg.com> (raw)
JSONB fields are very attractive for our current use, particularly as straight key-value pairs in the JSONB data;
but we are having trouble finding documentation on how to query lists (of scalars or objects) in nodes of the JSONB data.
~~~
I have the table as follows:
CREATE TABLE public.contacts
(
id integer NOT NULL DEFAULT nextval('contacts_id_seq'::regclass),
uuid uuid NOT NULL DEFAULT gen_random_uuid(),
vertical_id integer NOT NULL,
inboundlog_id integer NOT NULL,
email character varying(255) COLLATE pg_catalog."default" NOT NULL,
data jsonb NOT NULL,
created_at timestamp(0) without time zone,
updated_at timestamp(0) without time zone,
CONSTRAINT contacts_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
~~~
I have a record as follows:
INSERT INTO contacts
(uuid, vertical_id, inboundlog_id, email, data)
VALUES
(gen_random_uuid(), 1, 1, ‘phil@site.com',
'{"first”:"Phil","last”:"Peters”,"subscriptions”:[101,202,303]}')
How do I craft a query to find all subscribers to program 202?
~~~
I have another record as follows:
INSERT INTO contacts
(uuid, vertical_id, inboundlog_id, email, data)
VALUES
(gen_random_uuid(), 1, 1, ‘bob@domain.com',
'{"first”:"Bob","last”:"Baker”,"downloads":[{"date":"2018-01-01 00:00:00","pubid”:123},{"date":"2018-02-02 00:00:00","pubid”:456}]}')
How do I craft a query to find all contacts who have downloaded pubid 123?
TIA -
Geoff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: geoff@rxmg.com
Subject: Re: Working with JSONB data having node lists
In-Reply-To: <93BF39AF-F96B-4437-BECE-CD7D7815CF4E@rxmg.com>
References: <93BF39AF-F96B-4437-BECE-CD7D7815CF4E@rxmg.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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