w3resource

Redis Sorted Sets: ZRANGE

Redis ZRANGE Command

Redis ZRANGE command is used to return the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the lowest to the highest score. Lexicographical order is used for elements with equal score.

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.

Here are some common uses and scenarios for the "ZRANGE" command:

  • Leaderboards: Fetch a range of top-scoring players from a leaderboard.
  • Ranking Systems: Retrieve a specified range of ranked members based on their scores.
  • Pagination: Implement pagination by fetching a subset of sorted set elements.
  • Data Analysis: Retrieve a segment of data for analysis or reporting purposes.
  • Range Queries: Perform range-based queries to fetch elements within a specified index range efficiently.

Syntax:

ZRANGE KEY START STOP [WITHSCORES]

Available since

1.2.0.

Return Value

Array reply, a list of elements in the specified range (optionally with their scores).

Return Value Type

Array

Example: Redis ZRANGE

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> ZRANGE mycity 0 -1 WITHSCORES
 1) "Delhi"
 2) "1"
 3) "London"
 4) "2"
 5) "Paris"
 6) "3"
 7) "Tokyo"
 8) "4"
 9) "NewYork"
10) "5"
11) "Seoul"
12) "6"

Previous: ZLEXCOUNT
Next: ZRANGEBYLEX



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-zrange-key-start-stop.php