ZRANK(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ ZRANK ✨

okay bestie, welcome to the valkey command manual, page three of zrank, and honestly? this one is a whole vibe πŸ’…


🌸 NAME 🌸

zrank β€” returns the index of a member in a sorted set ordered by ascending scores. that's it, that's the girl πŸŽ€


✨ SYNOPSIS ✨

ZRANK key member [WITHSCORE]

πŸ’– DESCRIPTION πŸ’–

so basically, ZRANK returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. and the rank (aka the index) is 0-based, meaning the member with the lowest score gets rank 0. zero. the first one. no cap πŸ”₯

the optional WITHSCORE argument supplements the command's reply with the score of the element returned. so you get the rank and the score. it's giving two-for-one, and we love that 🫢

want the rank with scores ordered from high to low instead? that's ZREVRANK's whole job, go bother her πŸ’œ


πŸ¦‹ REPLY πŸ¦‹

RESP2

one of the following, bestie:

  • valkey-protocol(7) Nil reply: if the key does not exist or the member does not exist in the sorted set. sad girl hours 😭
  • valkey-protocol(7) Integer reply: the rank of the member when WITHSCORE is not used.
  • valkey-protocol(7) Array reply: the rank and score of the member when WITHSCORE is used.

RESP3

one of the following:

  • valkey-protocol(7) Null reply: if the key does not exist or the member does not exist in the sorted set. same energy, different name πŸ’€
  • valkey-protocol(7) Integer reply: the rank of the member when WITHSCORE is not used.
  • valkey-protocol(7) Array reply: the rank and score of the member when WITHSCORE is used.

⚑ COMPLEXITY ⚑

O(log(N)) β€” lowkey efficient, we stan 🌟


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@fast @read @sortedset


πŸŽ€ HISTORY πŸŽ€

  • Available since: 2.0.0 β€” she's been around, an icon, a legend ⭐
  • Changed in 7.2.0: Added the optional WITHSCORE argument. glow-up era 🌈

πŸ’… EXAMPLES πŸ’…

127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZRANK myzset "three"
(integer) 2
127.0.0.1:6379> ZRANK myzset "four"
(nil)
127.0.0.1:6379> ZRANK myzset "three" WITHSCORE
1) (integer) 2
2) "3"
127.0.0.1:6379> ZRANK myzset "four" WITHSCORE
(nil)

see how "three" pulls up at rank 2? main character behavior. and "four"? not invited, (nil), bye πŸ™ˆ


🌈 SEE ALSO 🌈

bzmpop(3valkey), bzpopmax(3valkey), bzpopmin(3valkey), zadd(3valkey), zcard(3valkey), zcount(3valkey), zdiff(3valkey), zdiffstore(3valkey), zincrby(3valkey), zinter(3valkey), zintercard(3valkey), zinterstore(3valkey), zlexcount(3valkey), zmpop(3valkey), zmscore(3valkey), zpopmax(3valkey), zpopmin(3valkey), zrandmember(3valkey), zrange(3valkey), zrangebylex(3valkey), zrangebyscore(3valkey), zrangestore(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrange(3valkey), zrevrangebylex(3valkey), zrevrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)


and that's the whole page, bestie!! this was valkey version 9.0.0, dated 2025-10-21, and you absolutely ate reading all of it. go forth and rank your sorted sets. periodt πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from