Redis Sorted Sets

Author: Al-mamun Sarkar Date: 2020-09-21 14:40:16

Redis sorted set is a sorted list of unique values used to store sets data structure in Redis under a key. I this lesson I will show the commands of the Redis sorted sets.

ZADD command is used to create a new set or add an item at the set:

Syntax:

ZADD key score1 value1 score2 value2 ...

Example:

ZADD names 1 Jone
ZADD names 2 Mamun
ZADD names 4 Sarkar 3 Khan

 

Some Other Commands of Sets:

Commands Description Syntax Example
Adding elements to the sorted set ZADD key score1 value score2 value ZADD names 4 Sarkar 3 Khan
Getting the number of members of a sorted set ZCARD key ZCARD names
Getting members within a range ZRANGE key start stop ZRANGE names 0 7
Getting all members ZRANGE key 0 -1 ZRANGE names 0 -1
Getting all members with score ZRANGE key 0 -1 WITHSCORES  ZRANGE names 0 -1 WITHSCORES 
Increment score of a member ZINCRBY KEY INCREMENT MEMBER ZINCRBY names 2 Jone
Getting elements by score range ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] ZRANGEBYSCORE names 3 5
ZRANGEBYSCORE names 3 5 WITHSCORES
ZRANGEBYSCORE names 3 7 WITHSCORES LIMIT 0 2
Getting the index of a value ZRANK key member ZRANK names Khan
Remove members from sorted set ZREM key member1 member2 ..... ZREM names Khan
Delete all members within the range ZREMRANGEBYRANK key start stop ZREMRANGEBYRANK names 0 2
Delete all member within range ZREMRANGEBYSCORE key min max ZREMRANGEBYSCORE names 0 2
Getting value on descending order ZREVRANGE key start stop [WITHSCORES] ZREVRANGE names 0 -1
ZREVRANGE names 0 -1 WITHSCORES
Getting score of a member ZSCORE key member ZSCORE names Jone