ZRANGE command in redis

ZRANGE key start end [WITHSCORES]

Return the specified elements of the sorted set at the specified key.

The elements are considered sorted from the lowerest to the highest score when using ZRANGE, and in the reverse order when using ZREVRANGE.

Start and end are zero-based indexes. 0 is the first element of the sorted set,1 is the next element by score, and so on.

TIME COMPLEXITY: O(log(N))+O(M) (with N being the number of elements in the sorted set and M the number of elements requested)

Note:

1. start and end can also be negative numbers indicating offsets from the end of the sorted set. For example, -1 is the last element of the sorted set, -2 is the penultimate element, and so on.

2. Indexes out of range will not produce an error: if start is over the end of the sorted set, or start > end, an empty list is returned. If the end is over at the end of the sorted set Redis will threaten it just like the last element of the sorted set.

RETURN VALUE: Multi bulk reply, specifically a list of elements in the specified range.

Syntax:

ZRANGE key start end [WITHSCORES]

Example 1: When the key is present and the range is not out of bound.

redis:6379> ZADD key1 1 A 1 B 2 C 3 D 4 E 4 F
(integer) 6
redis:6379> ZRANGE key1 0 3
1) "A"
2) "B"
3) "C"
4) "D"

Output

1) "A"

2) "B"

3) "C"

4) "D" 


Example 2: When the key is not present or the range is out of bound.

redis:6379> ZRANGE key1 10 20
(empty array)
redis:6379> ZRANGE myKey 0 3
(empty array)

Output

empty array


No comments:

Post a Comment