Summary of zset command of redis

Keywords: Database Redis

Summary of zset command of redis

zset(sorted set)
Redis zset, like set, is also a collection of string elements, and duplicate members are not allowed.
The difference is that each element is associated with a score of type double.
redis sorts the members of the collection from small to large through scores. The members of zset are unique, but the score can be repeated.

The redis ordered collection zset, like the collection set, is also a collection of string type elements, and duplicate members are not allowed.

The difference is that each element of zset is associated with a score (the score can be repeated). redis uses the score to sort the members of the set from small to large. Scores can be repeated

1.zadd

Add one or more member elements and their score values to the ordered set key. If the member exists in the set, update the value; Score can be an integer or a floating point number

#zadd key score member [score member...]
127.0.0.1:6379> zadd zs1 10 zhangsan 20 lisi 99 xiaoming
(integer) 3

2.zrem

Delete one or more members in the ordered set key. The nonexistent members are ignored. Return value: the number of members successfully deleted, excluding the ignored members.

#zrem key member [member...]
127.0.0.1:6379> zrem zs1 lisi ll
(integer) 1
127.0.0.1:6379> zrange zs1 0 -1
1) "zhangsan"
2) "xiaoming"

3.zcard

Gets the number of element members of the ordered set key

Return value: if the key exists, the number of collection elements is returned; if the key does not exist, 0 is returned

#zcard key
127.0.0.1:6379> zcard zs1
(integer) 3

4. zrange

Query the ordered set and specify the elements in the interval. Collection members are sorted by score value from small to large. Start and stop start from 0. 0 is the first element, 1 is the second element, and so on. Use - 1 for the last member and - 2 for the penultimate member. The with scores option returns score and value together. Return value: member set of self - defined interval

127.0.0.1:6379> zadd zs1 10 zhangsan 20 lisi 99 xiaoming
(integer) 3
#zrange key start end
127.0.0.1:6379> zrange zs1 0 -1
1) "zhangsan"
2) "lisi"
3) "xiaoming"

5. zrevrange

Returns the members in the specified interval in the ordered set key. The positions of members are arranged in descending order (from large to small) according to the score value. Others are the same as zrange command.

127.0.0.1:6379> zrange zs1 0 -1
1) "zhangsan"
2) "lisi"
3) "xiaoming"

#zrevrange key start stop [WITHSCORES]
127.0.0.1:6379> zrevrange zs1 0 -1
1) "xiaoming"
2) "lisi"
3) "zhangsan"

6.zrangebyscore

Get all members in the ordered set key whose score value is between min and max (including min and max). The ordered members are sorted in ascending order (from small to large).

min and max are included, and the symbol (excluding) is used. min and max can use - inf and + inf for minimum and maximum

Limit is used to limit the number and range of returned results.

With cores displays score and value

Return value: the collection data of the specified interval

127.0.0.1:6379> zadd zs 1 a 2 b 3 c 4 d 5 e 6 f
(integer) 6
127.0.0.1:6379> zrange zs 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"

#zrangebyscore key mix max
127.0.0.1:6379> zrangebyscore zs 1 3 
1) "a"
2) "b"
3) "c"

#zrangebyscore key mix max withscores
127.0.0.1:6379> zrangebyscore zs 1 3 withscores 
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"

#zrangebyscore key (mix max withscores
127.0.0.1:6379> zrangebyscore zs (1 3 withscores 
1) "b"
2) "2"
3) "c"
4) "3"

#zrangebyscore key mix (max withscores
127.0.0.1:6379> zrangebyscore zs 1 (3 withscores 
1) "a"
2) "1"
3) "b"
4) "2"

7.zrevrangebyscore

zrevrangebyscore key max min [WITHSCORES ] [LIMIT offset count]

Returns all members of the ordered set key whose score value is between Max and min (including those equal to max or min by default). The members of the ordered set are arranged in descending order of score value (from large to small). Others are the same as zrangebyscore

127.0.0.1:6379> zrevrange zs 0 -1 
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> zrevrange zs 0 -1 withscores
 1) "f"
 2) "6"
 3) "e"
 4) "5"
 5) "d"
 6) "4"
 7) "c"
 8) "3"
 9) "b"
10) "2"
11) "a"
12) "1"
127.0.0.1:6379> zrevrangebyscore zs 6 3 withscores 
1) "f"
2) "6"
3) "e"
4) "5"
5) "d"
6) "4"
7) "c"
8) "3"
127.0.0.1:6379> zrevrangebyscore zs (6 3 withscores 
1) "e"
2) "5"
3) "d"
4) "4"
5) "c"
6) "3"
127.0.0.1:6379> zrevrangebyscore zs 6 (3 withscores 
1) "f"
2) "6"
3) "e"
4) "5"
5) "d"
6) "4"
127.0.0.1:6379> zrevrangebyscore zs (6 (3 withscores 
1) "e"
2) "5"
3) "d"
4) "4"

8. zcount

zcount key min max

Returns the number of members in the ordered set key whose score value is between min and max (by default, the score value is equal to min or max)

127.0.0.1:6379> zcount zs 1 4
(integer) 4

9. zrank

zrank key member

Gets the ranking of the specified elements in the specified ordered collection (ranking starts from 0)

127.0.0.1:6379> zrank zs a
(integer) 0
127.0.0.1:6379> zrank zs b
(integer) 1
127.0.0.1:6379> zrank zs c
(integer) 2

10. zscore

zscore key member

Gets the score of the specified element in the specified ordered collection

127.0.0.1:6379> zscore zs a
"1"
127.0.0.1:6379> zscore zs b
"2"
127.0.0.1:6379> zscore zs c
"3"

Posted by hossein2kk on Sun, 24 Oct 2021 17:14:44 -0700