ZREM
The ZREM
command in DiceDB is used to remove the specified members from the sorted set stored at key and return the number of members removed from the sorted set. This command ignores the non-existing members.
Syntax
Parameters
Parameter | Description | Type | Required | Multiple |
---|---|---|---|---|
key | The key associated with the sorted set whose members are to be removed | String | Yes | No |
member | One or more members to remove from the sorted set | String | Yes | Yes |
Return Value
Condition | Return Value |
---|---|
If specified key and members exists | Count of members removed from the sorted set at key |
If key doesn’t exist | 0 |
If member doesn’t exist | 0 |
Behaviour
- DiceDB checks if the specified key exists.
- If the key exists and is associated with a sorted set, DiceDB removes the members from the sorted set and returns number of members removed.
- If the key does not exist, DiceDB returns
0
. - If the key exists but is not associated with a sorted set, an error is returned.
Errors
-
Wrong type of key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when attempting to use the command on a key that contains a non sorted set value.
- Error Message:
-
Wrong number of arguments
:- Error Message:
(error) -ERR wrong number of arguments for 'ZREM' command
- Occurs if key or member isn’t specified in the command.
- Error Message:
Example Usage
Basic Usage
Creating sorted set myzset
with fields one
, two
, three
, four
, five
with scores 1, 2, 3, 4, 5 respectively. Removing elements from myzset
.
Invalid Usage on non-existent sorted set
Removing element from a non-existent sorted set nonExistentZSet
.
Invalid Usage on a non sorted set key
Getting cardinality of a key mystring
associated with a non sorted set type.
Notes
- The
ZREM
command is a O(M*log(N)) time-complexity operation, with N being the number of elements in the sorted set and M the number of elements to be removed.