ZPOPMAX
The ZPOPMAX
command in DiceDB is used to remove and return the members with the highest scores from the sorted set data structure at the specified key. The second argument count is optional which specifies the number of elements that needs to be popped from the sorted set.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the sorted set data structure. If it does not exist, an empty array is returned. | String | Yes |
count | The count argument specifies the maximum number of members to return from highest to lowest. | Integer | No |
Return values
Condition | Return Value |
---|---|
If the key is of valid type and records are present | List of members including their scores |
If the key does not exist or if the sorted set is empty | (empty list or set) |
Behaviour
- The command first checks if the specified key exists.
- If the key does not exist, an empty array is returned.
- If the key exists but is not a sorted set, an error is returned.
- If the
count
argument is specified, up to that number of members with the highest scores are returned and removed. - The returned array contains the members and their corresponding scores in the order of highest to lowest.
Errors
-
Wrong type error
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when trying to use the command on a key that is not a sorted set.
- Error Message:
-
Syntax error
:- Error Message:
(error) ERROR wrong number of arguments for 'zpopMAX' command
- Occurs when the command syntax is incorrect or missing required parameters.
- Error Message:
-
Invalid argument type error
:- Error Message :
(error) ERR value is not an integer or out of range
- Occurs when the count argument passed to the command is not an integer.
- Error Message :
Examples
Non-Existing Key (without count argument)
Attempting to pop the member with the highest score from a non-existent sorted set:
Existing Key (without count argument)
Popping the member with the highest score from an existing sorted set:
With Count Argument
Popping multiple members with the highest scores using the count argument:
Count Argument but Multiple Members Have the Same Score
Popping members when multiple members share the same score:
Negative Count Argument
Attempting to pop members using a negative count argument:
Floating-Point Scores
Popping members with floating-point scores:
Wrong number of arguments
Attempting to pop from a key that is not a sorted set:
Invalid Count Argument
Using an invalid (non-integer) count argument:
Wrong Type of Key (without count argument)
Attempting to pop from a key that is not a sorted set: