I have the following json doc in MYSQL JSON field called test:
[
{"a": "1", "b": "2"},
{"a": "1", "b": "-2"},
{"a": "2", "b": "3"},
{"a": "2", "b": "-3"},
{"a": "3", "b": "4"}
]
CREATE TABLE `test` (`test` JSON);
INSERT INTO `test` VALUES
(JSON_ARRAY(JSON_OBJECT('a', '1', 'b', '2'),
JSON_OBJECT('a', '1', 'b', '-2'),
JSON_OBJECT('a', '2', 'b', '3'),
JSON_OBJECT('a', '2', 'b', '-3'),
JSON_OBJECT('a', '3', 'b', '4'))),
(JSON_ARRAY()),
(JSON_ARRAY());
SELECT JSON_UNQUOTE(JSON_SEARCH(`test`, 'all', 1, null, '$[*].a')) `data`
FROM `test`;
+----------------------+
| data |
+----------------------+
| ["$[0].a", "$[1].a"] |
| NULL |
| NULL |
+----------------------+
And I want to remove all dictionaries that have key/value "a": "1".
So I tried this:
UPDATE `test`
SET `test` = JSON_REMOVE(`test`, JSON_UNQUOTE(JSON_SEARCH(`test`,
'all',
1,
null,
'$[*].a')));
The expected result that I wanted is, but of course it doesn't work:
// This is an expected result after update
SELECT JSON_UNQUOTE(JSON_SEARCH(`test`, 'all', 1, null, '$[*].a')) `data`, `test` FROM `test`;
+----------------------+------------------------------------------------------------------------------------------------------------------+
| data | test |
+----------------------+------------------------------------------------------------------------------------------------------------------+
| NULL | [{"a": "2", "b": "3"}, {"a": "2", "b": "-3"}, {"a": "3", "b": "4"}] |
| NULL | [] |
| NULL | [] |
+----------------------+------------------------------------------------------------------------------------------------------------------+
Note that the mysql version is 8.0
Thank you