Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

ZRANK

Last updated
ZRANK key member [WITHSCORE]
Group
Sorted set
Complexity
O(log(N)) where N is the size of the sorted set
Arity
-3 (at least 3 words, counting the command name)
Flags
read-only, fast
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

ZRANK returns the position of member in the sorted set at key, ordered from the lowest score to the highest. The lowest-scored member has rank 0. For the rank from the highest score down, use ZREVRANK.

Option Effect
WITHSCORE Also return the member’s score.
Case RESP2 RESP3
Member exists Integer: the rank Same
Member exists, with WITHSCORE Two-element array: the rank (integer) and the score (bulk string) Two-element array: the rank (integer) and the score (double)
Member or key does not exist Nil bulk string Null
Member or key does not exist, with WITHSCORE Nil array Null

Errors:

  • ERR syntax error if the third argument is not WITHSCORE.
  • WRONGTYPE if key holds a value that is not a sorted set.
127.0.0.1:6379> ZADD board 100 "alice" 90 "bob" 95 "carol"
(integer) 3
127.0.0.1:6379> ZRANK board "carol"
(integer) 1
127.0.0.1:6379> ZRANK board "alice" WITHSCORE
1) (integer) 2
2) "100"
127.0.0.1:6379> ZRANK board "nobody"
(nil)

Same as Redis 7.4 and Valkey 8.

ZREVRANK, ZSCORE, ZRANGE