JSON.ARRINSERT
Syntax
JSON.ARRINSERT key path index value [value ...]
- Available in:
- Redis Stack / JSON 1.0.0
- Time complexity:
- O(N) when path is evaluated to a single value where N is the size of the array, O(N) when path is evaluated to multiple values, where N is the size of the key
将 json
值插入到 path
处的数组中,位于 index
之前(向右移动)
必需的参数
key
是修改的关键。
value
是一个或多个要插入到一个或多个数组中的值。
index
是数组中你想要插入值的位置。索引必须在数组的范围内。在index
0处插入会将值添加到数组的开头。负索引值从数组的末尾开始计算。
可选参数
path
是JSONPath指定的。默认是根 $
。
返回值
JSON.ARRINSERT
返回一个整数回复的数组,每个路径对应数组的新大小,或者如果匹配的JSON值不是数组,则返回nil
。
有关回复的更多信息,请参阅Redis序列化协议规范。
示例
Add new colors to a specific place in a list of product colors
创建一个关于黑色和银色降噪耳机的文档。
redis> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","connection":{"wireless":true,"type":"Bluetooth"},"price":99.98,"stock":25,"colors":["black","silver"]}'
OK
将颜色 blue
添加到 colors
数组的末尾。JSON.ARRAPEND
返回数组的新大小。
redis> JSON.ARRAPPEND item:1 $.colors '"blue"'
1) (integer) 3
返回colors
数组的新长度。
redis> JSON.GET item:1
"{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\",\"blue\"]}"
获取产品的颜色列表。
redis> JSON.GET item:1 '$.colors[*]'
"[\"black\",\"silver\",\"blue\"]"
在第二种颜色之后插入两种颜色。你现在有五种颜色。
redis> JSON.ARRINSERT item:1 $.colors 2 '"yellow"' '"gold"'
1) (integer) 5
获取更新后的颜色列表。
redis> JSON.GET item:1 $.colors
"[[\"black\",\"silver\",\"yellow\",\"gold\",\"blue\"]]"
另请参阅
JSON.ARRAPPEND
| JSON.ARRINDEX