zrank.3valkey - Man Page

Returns the index of a member in a sorted set ordered by ascending scores.

Synopsis

ZRANK key member [WITHSCORE]

Description

Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.

The optional WITHSCORE argument supplements the command’s reply with the score of the element returned.

Use ZREVRANK to get the rank of an element with the scores ordered from high to low.

Reply

Resp2

One of the following:

  • valkey-protocol(7) Nil reply: if the key does not exist or the member does not exist in the sorted set.
  • 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.
  • 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))

Acl Categories

@fast @read @sortedset

History

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 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), zrangestore(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)

Info

2024-09-23 8.0.0 Valkey Command Manual