Type: ZSet¶
ZSet is an disorderly, unrepeatable type, but it can be ordered by score.
-
When you add a member of Set, you need execute command like:
-
When you add a member of ZSet, you need bring the
score
field. Execute command like:
The key words of type ZSet are Can be ordered. So it is suitable for Rank List
scenarios or other about rank.
The unit of ZSet is score-member pair. Score means weights and used for sorting.
The members are unique. It is the main field. Members can not be duplicated.
And score is not unique. It is more like an auxiliary field.
By default, ZSet sort by score. Elements with the same score are ordered by lexicographically.
ZADD¶
- Syntax:
ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
- Description: Adds a score-member pair into zset which named
key
. The ZSet will be created if it does not exist.- NX | XX:
- NX: Only add new elements. Don't update already existing elements.
- XX: Only update elements that already exist. Don't add new elements.
- GT | LT:
- GT: Only update existing elements if the new score is greater than the current score. This flag doesn't prevent adding new elements.
- LT: Only update existing elements if the new score is less than the current score. This flag doesn't prevent adding new elements.
- CH: When use this option, return value means total number of elements CHanged (new elements added + the score was updated). Return value means number of elements added by default.
- INCR: When this option is specified ZADD acts like ZINCRBY. Only one score-element pair can be specified in this mode.
- NX | XX:
- Return: Number of successful added.
Note
- Add action: NX
- Update action: XX, GT, LT
- prevent adding: XX
- doesn't prevent adding: GT, LT
Example
Adding | |
---|---|
Only adding, update action will not be executed | |
---|---|
Only Update, adding action will not be executed | |
---|---|
- CH flag means return CHanged numbers.
Length¶
ZCARD¶
- Syntax:
ZCARD key
- Description: Returns the number of elements in the ZSet named
key
- Return: Number of elements, 0 if key is not exist.
ZCOUNT¶
- Syntax:
ZCOUNT key min max
- Description: Return the number of elements in the ZSet at
key
with score betweenmin
andmax
.- The
min
andmax
have the same semantic as describe for ZRANGE'sScore range
.
- The
ZLEXCOUNT¶
- Syntax:
ZLEXCOUNT key min max
- Description: Return the number of elements in the ZSet at
key
with lexicographical betweenmin
andmax
.- The
min
andmax
have the same semantic as describe for ZRANGE'sLexicographical range
.
- The
List¶
ZRANGE¶
ZRANGE can perfrom different types of range queries: by index(rank), by score, by lexicographical order.
- Syntax:
ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
-
Description: Lists the elements within the specified MIN and MAX ranges.
- The ranges includes minimum element and maximum element, equal to the start-with and end-with.
- WITHSCORES: Lists members with score. Only members are listed by default.
- BYSCORE: Lists members from min to max by score
- BYLEX: Lists members from min to max ly lexicographical order.
- REV: Reverses the result. The elements are ordered from low to high score by default.
-
LIMIT: Same like SELECT LIMIT offset count. It needs specified BYSCORE or BYLEX. A negative
count
will returns all members fromoffset
. -
Index range:
-
min | max: By default, the command perfroms an index range query.
- if min is greater than either the end index of the ZSet or max, an empty list is returns.
-
if max is greater than the end index of the ZSet, the index of last element will be used.
-
ZRANGE key 0 -1
equalZRANGE key 0 index-of-last-element
ZRANGE key 0 -2
equalZRANGE key 0 index-of-penultimate-element
ZRANGE key 11 5
return(empty list)
ZRANGE key 0 999
return0 <= index <= last-element
ZRANGE key (1 5
return1 < index <= 5
ZRANGE key 1 (5
return1 <= index < 5
ZRANGE key (1 (5
return1 < index < 5
-
-
Score range:
-
min | max: When the BYSCORE option is used,
-inf
and+inf
is vaild.-
-inf
and+inf
is a built-in variable mean infinite. -
if
min
is greater than either the highest score of the ZSet ormax
, an empty list is returns. -
if
max
is greater than the highest score of the ZSet, the score of highest score will be used. -
ZRANGE key 0 -1 BYSCORE
equalZRANGE key 0 index-of-last-element BYSCORE
ZRANGE key 0 -2 BYSCORE
equalZRANGE key 0 index-of-penultimate-element BYSCORE
ZRANGE key -inf +inf BYSCORE
return all elementZRANGE key 3 +inf BYSCORE
return3 <= score <= highest-score
ZRANGE key 5 -inf BYSCORE
return(empty list)
becausemin
is greater thanmax
-
-
-
Lexicographical range:
- start | stop:
- When the BYLEX option is used,
start
andstop
must start with(
or[
to specify whether the range interval is exclusive or inclusive.ZRANGE key (a [e BYLEX
returnb c d e
ZRANGE key a [e BYLEX
orZRANGE key [a e BYLEX
is invaild.
- The special value of
+
or-
forstart
orstop
mean positive or negative infinite strings.ZRANGE key - + BYLEX
returna b c d e
ZRANGE key + - BYLEX REV
returne d c b a
- When the BYLEX option is used,
- start | stop:
-
Conclusion - Min and Max:
- In a index range query, it is in the same way as Python index:
- 0 means First element, 1 means Second element,
- -1 means Last element, -2 means Penultimate element, and so on.
- In a score range query,
-inf
and+inf
is vaild, then-
and+
is invaild. - In a lexicographical range query,
-
and+
is vaild, then-inf
and+inf
is invaild. And it must start with(
or[
, except-
and+
.
- In a index range query, it is in the same way as Python index:
Example
Normal | |
---|---|
WITHSCORES | |
---|---|
ZRANGEBYSCORE¶
- Syntax:
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
- Description: Equal to
ZRANGE key min max BYSCORE
ZRANGEBYLEX¶
- Syntax:
ZRANGEBYLEX key min max [LIMIT offset count]
- Description: Equal to
ZRANGE key min max BYLEX
ZREVRANGE¶
- Syntax:
ZREVRANGE key start stop [WITHSCORES]
- Description: Equal to
ZRANGE key min max REV
ZREVRANGEBYSCORE¶
- Syntax:
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
- Description: Equal to
ZRANGE key min max BYSCORE REV
ZREVRANGEBYLEX¶
- Syntax:
ZREVRANGEBYLEX key max min [LIMIT offset count]
- Description: Equal to
ZRANGE key min max BYLEX REV
ZRANGESTORE¶
- Syntax:
ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
- Description: Equal to
ZRANGE key min max
Get¶
ZRANK¶
- Syntax:
ZRANK key member
- Description: Returns the index of the member in key. Nil when the member is not exist.
ZREVRANK¶
- Syntax:
ZREVRANK key member
- Description: Returns the index of the member after
key
is reversed. Nil whene the member is not exist.
ZSCORE¶
- Syntax:
ZSCORE key member
- Description: Returns the score of the member in key. Nil when the member is not exist.
ZMSCORE¶
- Syntax:
ZMSCORE key member [member ...]
- Description: Returns the scores associated with the specified members in the ZSet named
key
. - Return: Every member's score. Nil if any member is not exist.
Example
ZPOPMIN¶
- Syntax:
ZPOPMIN key [count]
- Description: Removes and returns up to
count
members with the lowest scores in the ZSet namedkey
.Count
is 1 by default.
Example
ZPOPMAX¶
- Syntax:
ZPOPMAX key [count]
- Description: Removes and returns up to
count
members with the highest scores in the ZSet namedkey
.Count
is 1 by default.
Example
ZRANDMEMBER¶
- Syntax:
ZRANDMEMBER key [count [WITHSCORES]]
- Description: Returns a random element from the ZSet named
key
.Count
is 1 by default.
Delete¶
ZREM¶
- Syntax:
ZREM key member [member ...]
- Description: Removes the special members from ZSet named
key
. - Return: 1 if removed successful, 0 either removed failed or members or key are not exist. error when
key
is not a ZSet.
ZREMRANGEBYLEX¶
- Syntax:
ZREMRANGEBYLEX key min max
- Description: Ranges the elements from
min
tomax
then removes them. Themin
andmax
have the same semantic as describe for ZRANGE'sLexicographical range
.
ZREMRANGEBYSCORE¶
- Syntax:
ZREMRANGEBYSCORE key min max
- Description: Ranges the elements from
min
tomax
then removes them. Themin
andmax
have the same semantic as describe for ZRANGE'sScore range
.
ZREMRANGEBYRANK¶
- Syntax:
ZREMRANGEBYRANK key start stop
- Description: Ranges the elements from
min
tomax
then removes them. Themin
andmax
have the same semantic as describe for ZRANGE'sIndex range
.
Calculation¶
ZINCRBY¶
- Syntax:
ZINCRBY key increment member
- Description: Increments the score of
member
in the ZSet namedkey
.