mysql - 使用 JSON_ARRAY_APPEND 将大数组插入 JSON 文档
问题描述
我正在使用JSON_ARRAY_APPEND
更新表中现有条目中的数组对象。
我通过代码和查询看起来如下:
UPDATE table t1
SET t1.value = JSON_ARRAY_APPEND('[]', '$', JSON_OBJECT('desc', '${desc}',
'hosts', '[${hosts}]'))
WHERE t1.key = '${key1}
我的结果value
如下所示:
[{"desc": "this is desc", "hosts": "[host1,host2, host3]"}]
所需的输出value
是:
[{"desc": "this is desc", "hosts": ["host1","host2","host3"]"}]
解决方案
``
SELECT CONCAT('"', REPLACE('${hosts}', ',', '","'), '"');
UPDATE table t1
SET t1.value = JSON_ARRAY_APPEND('[]', '$', JSON_OBJECT('desc', '${desc}', 'hosts', '[${hosts}]'))
WHERE t1.key = '${key1};
SET @host = 'host1,host2, host3';
UPDATE table t1
SET t1.value = JSON_ARRAY(
JSON_OBJECT(
'desc', 'this is desc',
'hosts',
JSON_ARRAY(
SUBSTRING_INDEX(@host, ',', 1),
SUBSTRING_INDEX(SUBSTRING_INDEX(@host, ',', 2), ',', -1) ,
SUBSTRING_INDEX(SUBSTRING_INDEX(@host, ',', 3), ',', -1)))) ;
''
推荐阅读
- arrays - 根据不同的列(或行)具有不同数据类型或混合精度的 Fortran 矩阵
- java - 泽西客户端连接已关闭
- python - 过滤日期范围不包括周末
- firebase - Firebase 托管需要多长时间才能工作?
- django - 嵌入式 django 站点的 CSP 设置似乎有问题
- python - 在 Python 中绘制多年的多个散点图
- javascript - 服务器标头上的 CORS
- php - 除非满足时间条件,否则禁用 WooCommerce 产品
- google-app-engine - gcloud app deploy failed: Docker image not found or not in V2 format
- java - 打印最大值和最小值,检查程序