w3resource

Redis Sorted Sets: ZREVRANGE

Redis ZREVRANGE Command

Redis ZREVRANGE 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 highest to the lowest score. The descending lexicographical order is used for elements with equal score.

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

  • Leaderboards: Fetch a range of players from a leaderboard in descending order of scores.
  • Ranking Systems: Retrieve a specified range of ranked members in descending order.
  • Top-N Queries: Retrieve the top N elements from a sorted set based on scores or ranks.
  • Data Analysis: Fetch a segment of data for analysis or reporting purposes in descending order.
  • Performance Optimization: Optimize data retrieval by fetching elements in reverse order based on scores or ranks.

Syntax:

ZREVRANGE key min max

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 ZREVRANGE

127.0.0.1:6379> ZADD mycityset 100000 Delhi 850000 Mumbai 700000 Hyderabad 800000 Kolkata
(integer) 4
127.0.0.1:6379> ZREVRANGE mycityset 0 -1 WITHSCORES
1) "Mumbai"
2) "850000"
3) "Kolkata"
4) "800000"
5) "Hyderabad"
6) "700000"
7) "Delhi"
8) "100000"

Example: Redis ZREVRANGE: Lookup specific range

127.0.0.1:6379> ZADD mycityset 100000 Delhi 850000 Mumbai 700000 Hyderabad 800000 Kolkata
(integer) 4
127.0.0.1:6379> ZREVRANGE mycityset 0 -1 WITHSCORES
1) "Mumbai"
2) "850000"
3) "Kolkata"
4) "800000"
5) "Hyderabad"
6) "700000"
7) "Delhi"
8) "100000"
127.0.0.1:6379> ZREVRANGE mycityset 0 1 WITHSCORES
1) "Mumbai"
2) "850000"
3) "Kolkata"
4) "800000"
127.0.0.1:6379> ZREVRANGE mycityset -2 -1 WITHSCORES
1) "Hyderabad"
2) "700000"
3) "Delhi"
4) "100000"

Previous: ZREMRANGEBYSCORE
Next: ZREVRANGEBYLEX



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-zrevrange-key-start-stop.php