LREM command in redis

LREM key count value

Remove the first count occurrences of the value element from the list.

Note:

1. If the count is zero all the elements are removed.

2. If the count is negative elements are removed from tail to head, instead to go from head to tail that is the normal behavior.

The number of removed elements is returned as an integer, see below for more information about the returned value. Note that non-existing keys are considered like empty lists by LREM, so LREM against non-existing keys will always return 0.

TIME COMPLEXITY: O(N) (with N being the length of the list)

RETURN VALUE: An integer reply containing the number of removed elements if the operation succeeded.

Syntax:

LREM KEY_NAME COUNT VALUE

Exmaple 1: When the key is present the value is also present

redis:6379> LPUSH myList hello
(integer) 1
redis:6379> LPUSH myList hello
(integer) 2
redis:6379> LPUSH myList 10
(integer) 3
redis:6379> LPUSH myList 20
(integer) 4
redis:6379> LREM myList 2 hello
(integer) 2

Output

2


Example 2: When the key is not present or value is not present

redis:6379> LREM myList 2 hello
(integer) 0

Output

0

No comments:

Post a Comment