w3resource

Redis Sorted Sets: ZRANGEBYLEX

ZRANGEBYLEX key min max [LIMIT offset count]

Redis ZRANGEBYLEX command is used to return all the elements in the sorted set at the key with a value between minimum and maximum, when all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering.

  • min, max are the range of the member. To view all the -, + use.
  • min, if you give the max value must be used '[' or '(' in the front. 
  • '[' Is used to contain a value, and '(' is used to block out. 

Both start and stop are zero-based indexes,

  • 0 is the first element,
  • 1 is the next element and so on.

They can also be negative numbers indicating offsets from the end of the sorted set,

  • -1 being the last element of the sorted set
  • -2 the penultimate element and so on.

Syntax:

ZRANGEBYLEX key min max [LIMIT offset count]

Available since

2.8.9.

Return Value

Array reply, a list of elements in the specified score range.

Return Value Type

Array

Example: Redis ZRANGEBYLEX

127.0.0.1:6379> ZADD mycity 1 Delhi 2 London 3 Paris 4 Tokyo 5 NewYork 6 Seoul
(integer) 6
127.0.0.1:6379> ZRANGE mycity 0 -1
1) "Delhi"
2) "London"
3) "Paris"
4) "Tokyo"
5) "NewYork"
6) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity - +
1) "Delhi"
2) "London"
3) "Paris"
4) "Tokyo"
5) "NewYork"
6) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity "[London" +
1) "London"
2) "Paris"
3) "Tokyo"
4) "NewYork"
5) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity "(London" +
1) "Paris"
2) "Tokyo"
3) "NewYork"
4) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity "(London" "(Seoul"
1) "Paris"

Example: Redis ZRANGEBYLEX: Use limit offset count

127.0.0.1:6379> ZADD mycity 1 Delhi 2 London 3 Paris 4 Tokyo 5 NewYork 6 Seoul
(integer) 6
127.0.0.1:6379> ZRANGE mycity 0 -1
1) "Delhi"
2) "London"
3) "Paris"
4) "Tokyo"
5) "NewYork"
6) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity - + LIMIT 0 2
1) "Delhi"
2) "London"
127.0.0.1:6379> ZRANGEBYLEX mycity - + LIMIT 2 3
1) "Paris"
2) "Tokyo"
3) "NewYork"

Previous: ZRANGE
Next: ZRANGEBYSCORE



Follow us on Facebook and Twitter for latest update.