Storing Set in Redis: Modification Commands

Learn how to modify data in a Set in Redis

SUNION command

As the name suggests, the SUNION command is used to find the union of two or more sets.

SUNION key1 key2 key3 …

In the example below, we have found the union of the fruits and fruits1 keys.

SREM command

If we need to remove some elements from the set, then we can use SREM command.

SREM key value1 value2 …

In the example below, we are removing apple from our fruits set. When the command is executed, 1 is returned, which indicates that one fruit was removed.

We can also remove multiple values using this command.

SPOP command

This command is used to remove a random value from the set. We can remove one or more random values.

SPOP key count

In the example below, we are removing one random value. When the command is executed, it returns the name of the value that was removed.

SMOVE command

The SMOVE command is used to move a value from one set to another.

Currently, we have two sets, i.e., fruits and fruits1. The elements in each of these sets are shown below.

We will move the fruit from one set to another using the SMOVE command.

SMOVE source dest member

Get hands-on with 1400+ tech skills courses.