I have the following JSON in MariaDB/MySQL:
[{"uid": 5}, {"uid": 6}, {"uid": 7}]
user_pst_tb
------------------------------
pst_id | pst_liked_by
--------------------------
1 |[]
-------|----------------
. |[{"uid": 9}]
-------|----------------
. |[]
-------|----------------
29 |[]
-------|----------------
30 | [{"uid": 5}, {"uid": 6}, {"uid": 7}]
i want to use JSON_REMOVE or any method to remove {"uid": 6} alone on pst_id = 30, but i cannot find how to formulate the path. I thought of this:
UPDATE user_pst_tb
SET `pst_liked_by` = JSON_REMOVE(
`pst_liked_by`, JSON_UNQUOTE(
REPLACE(
JSON_SEARCH( `pst_liked_by`, 'one', '6', null, '$**.uid' )
, '.uid'
, ''
)
)
) WHERE pst_id = 30;
for some reason the MariaDB and MySQL docs does not have such examples. Any help is appreciated.
I have also tried:
UPDATE user_pst_tb SET `pst_liked_by`= JSON_REMOVE(`pst_liked_by`, JSON_UNQUOTE( JSON_SEARCH(`pst_liked_by`, 'one','{"uid": 6}') )) WHERE `pst_id` = 30;
The second query clears all the JSON data sadly
UPDATE 1 (some GOOD NEWS) I have tried this
UPDATE user_pst_tb SET `pst_liked_by` =
JSON_REMOVE(`pst_liked_by`,JSON_UNQUOTE(JSON_search(`pst_liked_by`,
'one', '6'))) WHERE `pst_id` = 30;
Somehow working but it leaves some empty {} behind.
Example: [{"uid": 5}, {}, {"uid": 7}] any idea to remove the empty brackets will be great!!
JSON_REMOVEand predicates seems the right way, but I haven;t worked out the incantation.