JSONB operator clarification

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: PostgreSQL-documentation <pgsql-docs(at)postgresql(dot)org>
Subject: JSONB operator clarification
Date: 2015-10-06 00:19:26
Message-ID: 20151006001926.GA9634@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

In studying our JSONB operators, I was confused about what they do based
on the docs. I found that "contain within" means "contain the
path/value", i.e. it has to match the path from the top level, not just
anywhere inside the document:

SELECT '{"a":1, "c" : {"b":2}}'::jsonb @> '{"a":1}'::jsonb;
?column?
----------
t

SELECT '{"a":1, "c" : {"b":2}}'::jsonb @> '{"b":2}'::jsonb;
?column?
----------
f

You can also specify only the top part of the path:

SELECT '{"a":1, "c" : {"b":2}}'::jsonb @> '{"c":{}}'::jsonb;
?column?
----------
t

SELECT '{"a":1, "c" : {"b":2}}'::jsonb @> '{"d":{}}'::jsonb;
?column?
----------
f

I also found that "key/element string exist" really means "string exist
as a top-level key", e.g.

SELECT '{"a":1, "c" : {"b":2}}'::jsonb ? 'a';
?column?
----------
t

SELECT '{"a":1, "c" : {"b":2}}'::jsonb ? 'b';
?column?
----------
f

The attached doc patch and SQL comment update improves this.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription +

Attachment Content-Type Size
json.diff text/x-diff 6.2 KB

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Bruce Momjian 2015-10-06 00:56:54 Re: Omission of "(or C)" in psql's "pset title" header
Previous Message Bruce Momjian 2015-10-05 17:38:56 Re: Update docs for GIN index improvements