ZRANK
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key of the sorted set. | String | Yes |
member | The member whose rank is to be determined. | String | Yes |
WITHSCORE | If provided, the command will also return the score of the member. | String | No |
Return values
Condition | Return Value |
---|---|
If member exists in the sorted set | Integer (rank of the member) |
If WITHSCORE option is used | Array (rank and score of the member) |
If member or key does not exist | nil |
Behaviour
- The
ZRANK
command searches for the specified member within the sorted set associated with the given key. - If the key exists and is a sorted set, the command returns the rank of the member based on its score, with the lowest score having rank 0.
- If the
WITHSCORE
option is provided, the command returns both the rank and the score of the member as an array. - If the key does not exist or the member is not found in the sorted set, the command returns
nil
.
Errors
-
Wrong type of value or key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when the specified key exists but is not associated with a sorted set.
- Error Message:
-
Invalid syntax or number of arguments
:- Error Message:
(error) ERR wrong number of arguments for 'zrank' command
- Occurs if the command is issued with an incorrect number of arguments.
- Error Message:
-
Invalid option
:- Error Message:
(error) ERR syntax error
- Occurs if an invalid option is provided.
- Error Message:
Example Usage
Basic Usage
Retrieve the rank of member1
in the sorted set myzset
:
Using WITHSCORE Option
Retrieve both the rank and the score of member2
in the sorted set myzset
:
Best Practices
- Use
ZRANK
in combination withZADD
andZSCORE
for efficient management of sorted sets and leaderboards.
Notes
- This command is particularly useful for implementing leaderboards, pagination in ranked lists, and analytics on data distribution.