JSONB operator clarification

Started by Bruce Momjianover 10 years ago2 messagesdocs
Jump to latest
#1Bruce Momjian
bruce@momjian.us

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@momjian.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                             +

Attachments:

json.difftext/x-diff; charset=us-asciiDownload+27-27
#2Bruce Momjian
bruce@momjian.us
In reply to: Bruce Momjian (#1)
Re: JSONB operator clarification

On Mon, Oct 5, 2015 at 08:19:26PM -0400, Bruce Momjian wrote:

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:

Applied and backpatched to 9.5. No catalog bump as the catalog changes
are only for SQL comments.

--
Bruce Momjian <bruce@momjian.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                             +

--
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs