Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Zrangebylex Command

Redis Zrangebylex Command

Redis Sorted Set

The Redis Zrangebylex command is used to get members within a specified range in a sorted set by lexicographical order. This command is mainly used for sorted sets whose members are strings.

Syntax

The basic syntax of the redis Zrange command is as follows:



ZRANGEBYLEX key min max [LIMIT offset count]

Command behavior:

Available Version

= 2.8.9

Return Value

A list of elements within the specified range.

Example



redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g

(integer) 7

redis 127.0.0.1:6379> ZRANGEBYLEX myzset - [c

1) "a"

2) "b"

3) "c"

redis 127.0.0.1:6379> ZRANGEBYLEX myzset - (c

1) "a"

2) "b"

redis 127.0.0.1:6379> ZRANGEBYLEX myzset [aaa (g

1) "b"

2) "c"

3) "d"

4) "e"

5) "f"

redis> 

Suppose we have a sorted set myzset containing the following elements:



ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g

Get all elements with lexicographic order between a and f:



ZRANGEBYLEX myzset a f

Get all elements with lexicographic order greater than b and less than f:



ZRANGEBYLEX myzset [b (f

Get elements with lexicographic order between a and f, but return only the first 3:



ZRANGEBYLEX myzset a f LIMIT 0 3

Note that the ZRANGEBYLEX command performs range queries by lexicographical order, not by score. This makes it very suitable for scenarios that require retrieving data in alphabetical order.

Redis Sorted Set