1. zadd key [NX|XX] [CH] [INCR] score member [score member …]

Add member to key; sore is a double precision floating-point number string.

Both + INF (Max) and -INF (min) are valid values.

  • XX: Updates only existing members and does not add new members.
  • NX: Does not update existing members. Only new members are added.
  • CH: Modifies the return value to the total number of members that have changed
  • INCR: When ZADD specifies this option, the member’s operation is equivalent to the ZINCRBY command, which increments the member’s score.

The members are not duplicated, but the scores can be the same, and the scores are sorted lexicographically, comparing byte arrays of strings

redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 1 "uno"
(integer) 1
redis> ZADD myzset 2 "two" 3 "three"
(integer) 2
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"
redis> 
Copy the code

2. ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

  • Returns all elements in the ordered set of keys whose fraction is between min and Max (including elements whose fraction is equal to Max or min).
  • Elements are considered to be sorted from low to high.
  • The optional LIMIT argument specifies the number and range of results to return
  • The optional WITHSCORES argument returns the element and its score, not just the element
  • Closed ranges are used by default, but you can optionally use open ranges by prefixing () to arguments
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZRANGEBYSCORE myzset -inf +inf
1) "one"
2) "two"
3) "three"
redis> ZRANGEBYSCORE myzset 1 2
1) "one"
2) "two"
redis> ZRANGEBYSCORE myzset (1 2
1) "two"
redis> ZRANGEBYSCORE myzset (1 (2
(empty list or set)
redis> 
Copy the code

3. zrem key member [member…]

An error is returned when key exists but is not of an ordered collection type.

redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREM myzset "two"
(integer) 1
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "three"
4) "3"
redis> 

Copy the code

4. zcard key

Returns the number of ordered set elements for key.

redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZCARD myzset
(integer) 2
redis> 
Copy the code