JSON.ARRTRIM
Syntax
JSON.ARRTRIM key path start stop
- 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
修剪数组,使其仅包含指定的包含范围的元素
必需的参数
key
是修改的关键。
可选参数
path
是JSONPath指定的。默认是根 $
。
start
是保留的第一个元素的索引(之前的元素被修剪)。默认值为0。
stop
是要保留的最后一个元素的索引(后续元素被修剪),包括最后一个元素。默认值为0。负值被解释为从末尾开始。
About out-of-range indexes:
JSON.ARRTRIM 非常宽容,使用超出范围的索引不会产生错误。请注意 RedisJSON v2.0 和旧版本在处理超出范围的索引时的一些差异。
自 RedisJSON v2.0 起的行为:
- 如果
start
大于数组的大小或start
>stop
,则返回 0 和一个空数组。 - 如果
start
小于 0,则从数组的末尾开始。 - 如果
stop
大于数组的末尾,则将其视为最后一个元素。
返回
JSON.ARRTRIM 返回一个整数数组,每个路径对应数组的新大小,如果匹配的 JSON 值不是数组,则返回 nil
。
有关回复的更多信息,请参阅 Redis 序列化协议规范。
示例
Trim an array to a specific set of values
创建两个具有最大音量的耳机产品。
redis> JSON.SET key $
"[{\"name\":\"Healthy headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"max_level\":[60,70,80]},{\"name\":\"Noisy headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"max_level\":[85,90,100,120]}]"
OK
向第二个产品添加新的声级值。
redis> JSON.ARRAPPEND key $.[1].max_level 140 160 180 200 220 240 260 280
1) (integer) 12
获取更新后的数组。
redis> JSON.GET key $.[1].max_level
"[[85,90,100,120,140,160,180,200,220,240,260,280]]"
仅保留第五个到第九个元素之间的值,包括最后一个元素。
redis> JSON.ARRTRIM key $.[1].max_level 4 8
1) (integer) 5
获取更新后的数组。
redis> JSON.GET key $.[1].max_level
"[[140,160,180,200,220]]"
另请参阅
JSON.ARRINDEX
| JSON.ARRINSERT