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