JSON.ARRINDEX

Syntax
JSON.ARRINDEX key path value [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

在数组中搜索JSON值的首次出现

示例

必需的参数

key

是解析的关键。

path

是JSONPath指定的。

value

是在一个或多个数组中查找其索引的值。

关于在JSON命令中使用字符串:
要将字符串指定为数组值以进行索引,请用一组额外的单引号将引用的字符串括起来。示例:'"silver"'。有关更详细的使用,请参见示例

可选参数

start

是包含的起始值,用于指定要搜索的数组切片。默认值为 0

stop

是用于指定数组切片中搜索的独占停止值,包括最后一个元素。默认值为0。负值被解释为从末尾开始。

关于超出范围的索引:
超出范围的索引会环绕到数组的开始和结束。一个反向的索引范围(例如从1到0的范围)会返回未找到或-1

返回值

JSON.ARRINDEX 返回一个整数回复的数组,每个路径对应数组中的每个JSON值的第一个位置,如果在数组中未找到则返回-1,如果匹配的JSON值不是数组则返回nil。 有关回复的更多信息,请参阅Redis序列化协议规范

示例

Find the specific place of a color 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\"]]"

找到颜色silver所在的位置。

redis> JSON.ARRINDEX item:1 $..colors '"silver"'
1) (integer) 1

另请参阅

JSON.ARRAPPEND | JSON.ARRINSERT


RATE THIS PAGE
Back to top ↑