Redis Datatypes and Commands-Sorted Set

Redis Datatypes and Commands – Sorted 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

Sorted set

The Sorted Set datatype is used to maintain the order of the elements in ascending order.

zadd

The ZADD command is used to add the data into the key set. Use the below command to add the data into the key set.

Syntax
zadd <keyname> <score1> <member1> <score2> <member2> ... N

Example
zadd players 20 John 30 Amar 10 Mike
ZADD command

zrange

The ZRANGE command is used to view all the elements from the ordered key set. Use the below command to view all the elements from the key set.

Syntax
zrange <keyname> start stop

Example
zrange players 0 -1
ZRANGE command

The Start and Stop represents the index of the elements. The 0 to -1 represents to view all the elements from the set.

Execute the below command, to view all the elements from the set with “WITHSCORES” option.

Syntax
zrange <keyname> start stop withscores

Example
zrange players 0 -1 withscores
Zrange with scores

zcard

The ZCARD command is used to return the number of elements from the ordered key set. Use the below command to view the number of elements from the set.

Syntax
zcard <keyname>

Example
zcard players
ZCARD command

zcount

The ZCOUNT command is used to return the count of elements from the ordered key set. Use the below command to view the count of elements from the key set.

Syntax
zcount <keyname> -inf +inf

Example
zcount players -inf +inf
ZCOUNT command

zrank

The ZRANK command is used to find out the index of the element in the ordered key set. Use the below command to get the index of the element.

Syntax
zrank <keyname> member

Example
zrank players Amar
ZRANK command

zscore

The ZSCORE command is used to return the score of the member from the ordered key set. Use the below command to return the score of the member.

Syntax
zscore <keyname> member

Example
zscore players Amar
ZSCORE command

zrem

The ZREM command is used to remove the elements from the ordered set. Use the below command to remove the elements from the set.

Syntax
zrem <keyname> member

Example
zrem players Amar
ZREM command

Execute the below command to get the all the elements from the ordered set after remove the member from the key set.

zrange players 0 -1
ZRANGE command

We have learned and executed the Redis Sorted Set datatype commands.

Advertisements

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.