Redis Datatypes and Commands-Set

Redis Datatypes and Commands – Set

Redis stores the data as key value pair. Keys can be any binary value and it is represented as ASCII strings for easily understandable and readability purpose. Redis supports five different datatypes and anyone of the datatype can be represented as value.

datatypes

Redis supports five different datatypes and they are given below. Each datatype can be used in Redis with different set of commands.

  • String
  • List
  • Set
  • Sorted Set
  • Hash

set

Set is a one of the Redis Datatype and it is a collection of unique strings stored in an unordered list. Set doesn’t allow duplicate data in a key.

sadd

The SADD command is used to store the data into the set by using a key. Use the below command to add the data into the set.

Syntax
sadd <keyname> <member1> <member2> <member3>...N

Example
sadd notes note1 note2 note3
SADD command

smembers

The SMEMBERS command is used to get the data from the set by using a key. Use the below command to get the data from the set.

Syntax
smembers <keyname>

Example
smembers notes
SMEMBERS command

scard

The SCARD command is used to get the number of elements count from the set by using a key. Use the below command to get the number of elements count from the set.

Syntax
scard <keyname>

Example
scard notes
SCARD command

sdiff

The SDIFF command is used to find out the difference between two key sets.

Let us add the new key set and will execute the SDIFF command.

 sadd new_notes note3 note4 note5 
SADD command

Use the below command to find out the difference between two key sets.

Syntax
sdiff <keyname1> <keyname2>

Example
sdiff notes new_notes
SDIFF command

Try out to find the difference between two key set in another way.

sdiff notes new_notes notes
SDIFF command

sunion

The SUNION command is used to return all unique data between two key sets. Use the below command to return all unique data between two key sets.

Syntax
sunion <keyname1> <keyname2>

Example
sunion notes new_notes
SUNION command

sinter

The SINTER command is used to return the common data between two key sets. Use the below command to return the common data between the two keys.

Syntax
sinter <keyname1> <keyname2>

Example
sinter notes new_notes
SINTER command

spop

The SPOP command is used to remove the random data from the set. Use the below command to remove the random data from the key set.

Syntax
spop <keyname>

Example
spop notes
SPOP command

srem

The SREM command is used to remove the one or multiple data from the key set. Use the below command to remove the data from the key set.

Syntax
srem <keyname> <member1> <member2>..N

Example
srem new_notes note4 note5
SREM command

After remove the data from the key set, and execute the below command to see the remaining elements from the key set.

smembers notes
SMEMBERS command

We have learned and executed the Redis Set datatype and it’s commands.

How useful was this post?

Click on a star to rate it!

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

Advertisements

Post your comments