JSON.ARRPOP

Syntax
JSON.ARRPOP key [path [index]]
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 and the specified index is not the last element, O(1) when path is evaluated to a single value and the specified index is the last element, or O(N) when path is evaluated to multiple values, where N is the size of the key

从数组的索引中移除并返回一个元素

示例

必需的参数

key

是修改的关键。

index

是数组中开始弹出的位置。默认是 -1,表示最后一个元素。超出范围的索引会四舍五入到数组的相应末端。弹出空数组返回 null。

可选参数

path

是JSONPath指定的。默认是根 $

返回

JSON.ARRPOP 返回每个路径的数组批量字符串回复,每个回复是弹出的JSON值,如果匹配的JSON值不是数组,则返回nil。 有关回复的更多信息,请参阅Redis序列化协议规范

示例

Pop a value from an index and insert a new value

创建两个具有最大音量的耳机产品。

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":[80,90,100,120]}]'
OK

获取第二个产品的所有最大值。

redis> JSON.GET key $.[1].max_level
"[[80,90,100,120]]"

更新产品的max_level字段:删除一个不可用的值并添加一个新可用的值。

redis> JSON.ARRPOP key $.[1].max_level 0
1) "80"

获取更新后的数组。

redis> JSON.GET key $.[1].max_level
"[[90,100,120]]"

现在插入一个新的最小值。

redis> JSON.ARRINSERT key $.[1].max_level 0 85
1) (integer) 4

获取更新后的数组。

redis> JSON.GET key $.[1].max_level
"[[85,90,100,120]]"

另请参阅

JSON.ARRAPPEND | JSON.ARRINDEX


RATE THIS PAGE
Back to top ↑