aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-10-29 18:54:35 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2015-10-29 18:54:44 -0400
commit9a1a22980d3650e6e232bc4423ec74bfc6d0e7be (patch)
tree9f8c7158c540890ab190231f430cee49dc993051
parent0bc3071796b33288cd912db196b90c76fa394c21 (diff)
downloadpostgresql-9a1a22980d3650e6e232bc4423ec74bfc6d0e7be.tar.gz
postgresql-9a1a22980d3650e6e232bc4423ec74bfc6d0e7be.zip
Docs: add example clarifying use of nested JSON containment.
Show how this can be used in practice to make queries simpler and more flexible. Also, draw an explicit contrast to the existence operator, which doesn't work that way. Peter Geoghegan and Tom Lane
-rw-r--r--doc/src/sgml/json.sgml28
1 files changed, 28 insertions, 0 deletions
diff --git a/doc/src/sgml/json.sgml b/doc/src/sgml/json.sgml
index 118fb35e254..3cf78d6394c 100644
--- a/doc/src/sgml/json.sgml
+++ b/doc/src/sgml/json.sgml
@@ -349,6 +349,34 @@ SELECT '"foo"'::jsonb ? 'foo';
need to be searched linearly.
</para>
+ <tip>
+ <para>
+ Because JSON containment is nested, an appropriate query can skip
+ explicit selection of sub-objects. As an example, suppose that we have
+ a <structfield>doc</> column containing objects at the top level, with
+ most objects containing <literal>tags</> fields that contain arrays of
+ sub-objects. This query finds entries in which sub-objects containing
+ both <literal>"term":"paris"</> and <literal>"term":"food"</> appear,
+ while ignoring any such keys outside the <literal>tags</> array:
+<programlisting>
+SELECT doc-&gt;'site_name' FROM websites
+ WHERE doc @&gt; '{"tags":[{"term":"paris"}, {"term":"food"}]}';
+</programlisting>
+ One could accomplish the same thing with, say,
+<programlisting>
+SELECT doc-&gt;'site_name' FROM websites
+ WHERE doc-&gt;'tags' @&gt; '[{"term":"paris"}, {"term":"food"}]';
+</programlisting>
+ but that approach is less flexible, and often less efficient as well.
+ </para>
+
+ <para>
+ On the other hand, the JSON existence operator is not nested: it will
+ only look for the specified key or array element at top level of the
+ JSON value.
+ </para>
+ </tip>
+
<para>
The various containment and existence operators, along with all other
JSON operators and functions are documented