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
scorefield. 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
keywith score betweenminandmax.- The
minandmaxhave 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
keywith lexicographical betweenminandmax.- The
minandmaxhave 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
countwill 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 -1equalZRANGE key 0 index-of-last-element ZRANGE key 0 -2equalZRANGE key 0 index-of-penultimate-elementZRANGE key 11 5return(empty list)ZRANGE key 0 999return0 <= index <= last-elementZRANGE key (1 5return1 < index <= 5ZRANGE key 1 (5return1 <= index < 5ZRANGE key (1 (5return1 < index < 5
-
-
Score range:
-
min | max: When the BYSCORE option is used,
-infand+infis vaild.-
-infand+infis a built-in variable mean infinite. -
if
minis greater than either the highest score of the ZSet ormax, an empty list is returns. -
if
maxis greater than the highest score of the ZSet, the score of highest score will be used. -
ZRANGE key 0 -1 BYSCOREequalZRANGE key 0 index-of-last-element BYSCORE ZRANGE key 0 -2 BYSCOREequalZRANGE key 0 index-of-penultimate-element BYSCOREZRANGE key -inf +inf BYSCOREreturn all elementZRANGE key 3 +inf BYSCOREreturn3 <= score <= highest-scoreZRANGE key 5 -inf BYSCOREreturn(empty list)becauseminis greater thanmax
-
-
-
Lexicographical range:
- start | stop:
- When the BYLEX option is used,
startandstopmust start with(or[to specify whether the range interval is exclusive or inclusive.ZRANGE key (a [e BYLEXreturnb c d eZRANGE key a [e BYLEXorZRANGE key [a e BYLEXis invaild.
- The special value of
+or-forstartorstopmean positive or negative infinite strings.ZRANGE key - + BYLEXreturna b c d eZRANGE key + - BYLEX REVreturne 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,
-infand+infis vaild, then-and+is invaild. - In a lexicographical range query,
-and+is vaild, then-infand+infis 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
keyis 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
countmembers with the lowest scores in the ZSet namedkey.Countis 1 by default.
Example
ZPOPMAX¶
- Syntax:
ZPOPMAX key [count] - Description: Removes and returns up to
countmembers with the highest scores in the ZSet namedkey.Countis 1 by default.
Example
ZRANDMEMBER¶
- Syntax:
ZRANDMEMBER key [count [WITHSCORES]] - Description: Returns a random element from the ZSet named
key.Countis 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
keyis not a ZSet.
ZREMRANGEBYLEX¶
- Syntax:
ZREMRANGEBYLEX key min max - Description: Ranges the elements from
mintomaxthen removes them. Theminandmaxhave the same semantic as describe for ZRANGE'sLexicographical range.
ZREMRANGEBYSCORE¶
- Syntax:
ZREMRANGEBYSCORE key min max - Description: Ranges the elements from
mintomaxthen removes them. Theminandmaxhave the same semantic as describe for ZRANGE'sScore range.
ZREMRANGEBYRANK¶
- Syntax:
ZREMRANGEBYRANK key start stop - Description: Ranges the elements from
mintomaxthen removes them. Theminandmaxhave the same semantic as describe for ZRANGE'sIndex range.
Calculation¶
ZINCRBY¶
- Syntax:
ZINCRBY key increment member - Description: Increments the score of
memberin the ZSet namedkey.