]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - README.md
Changes:
[postgresql/pg_jsonb_opx] / README.md
1 jsonb_opx
2 =========
3
4 Test delete and concatenation operators for PostgreSQL 9.4, this is purely for experimentation and contain errors and bad form.  
5
6 **USE ON PRODUCTION SYSTEMS AT OWN RISK**
7
8 * delete operator **"-"** provided by functions *jsonb_delete(jsonb, text) jsonb_delete(jsonb, text[]) and jsonb_delete(jsonb, jsonb)*
9     Provides:
10         jsonb - text
11         jsonb - text[]
12         jsonb - jsonb
13
14     Note: When using text type operators on jsonb arrays only elements of type text will be deleted. E.g.
15
16 ```sql
17 TEST=# SELECT '[1, "1", "2", 2]'::jsonb - '2'::text;
18   ?column?   
19 -------------
20  [1, "1", 2]
21 (1 row)
22
23 TEST=# SELECT '[1, "1", "2", 2]'::jsonb - '"2"'::text;
24      ?column?     
25 ------------------
26  [1, "1", "2", 2]
27 (1 row)
28
29 TEST=# SELECT '[1, "1", "2", 2]'::jsonb - array['2']::text[];
30   ?column?   
31 -------------
32  [1, "1", 2]
33 (1 row)
34
35 TEST=# SELECT '[1, "1", "2", 2]'::jsonb - array['"2"']::text[];
36      ?column?     
37 ------------------
38  [1, "1", "2", 2]
39 (1 row)
40
41 ```
42
43     More. E.g.
44  
45 ```sql
46 TEST=# SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - 'b'::text;
47      ?column?     
48 ------------------
49  {"a": 1, "c": 3}
50 (1 row)
51
52 TEST=# SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b'];
53  ?column? 
54 ----------
55  {"c": 3}
56 (1 row)
57
58 TEST=# SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
59      ?column?     
60 ------------------
61  {"a": 1, "c": 3}
62 (1 row)
63
64 TEST=# SELECT '{"a": 1, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"d": {"a": 4}, "b": 2}'::jsonb;
65      ?column?     
66 ------------------
67  {"a": 1, "c": 3}
68 (1 row)
69
70 TEST=# SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
71         ?column?         
72 -------------------------
73  {"c": 3, "d": {"a": 4}}
74 (1 row)
75 ```
76     
77 * concatenation operator  **"||"** provided by function *jsonb_concat(jsonb, jsonb)*
78     Provides:
79          jsonb || jsonb
80
81     E.g.
82
83 ```sql
84 TEST=#  SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '{"a": 4, "b": 2, "d": 4}'::jsonb;
85              ?column?             
86 ----------------------------------
87  {"a": 4, "b": 2, "c": 3, "d": 4}
88 (1 row)
89
90 TEST=#  SELECT '["a", "b"]'::jsonb || '["c"]'::jsonb;
91     ?column?     
92 -----------------
93  ["a", "b", "c"]
94 (1 row)
95
96 TEST=#  SELECT '[1,2,3]'::jsonb || '[3,4,5]'::jsonb;
97       ?column?      
98 --------------------
99  [1, 2, 3, 3, 4, 5]
100 (1 row)
101
102 TEST=#  SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '[1,2,3]'::jsonb;
103               ?column?               
104 -------------------------------------
105  [{"a": 1, "b": 2, "c": 3}, 1, 2, 3]
106 (1 row)
107
108 TEST=#  SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '[1,2,3]'::jsonb || '"a"'::jsonb;
109                  ?column?                 
110 ------------------------------------------
111  [{"a": 1, "b": 2, "c": 3}, 1, 2, 3, "a"]
112 (1 row)
113 ```