获取排序集中的所有成员

我有一个已排序的集合,并希望获取集合的所有成员。如何识别命令的最大/最小得分:

zrange key min max

47901 次浏览

You're in luck, as zrange does not take scores, but indices. 0 is the first index, and -1 will be interpreted as the last index:

zrange key 0 -1

To get a range by score, you would call zrangebyscore instead -- where -inf and +inf can be used to denote negative and positive infinity, respectively, as Didier Spezia notes in his comment:

zrangebyscore key -inf +inf

In newer versions of redis (>= v6.2.0), if you want to get all members of a sorted set between two scores, you should use:

ZRANGE key min max BYSCORE

Adding the BYSCORE option makes redis treat the min & max arguments as scores rather than indices.

(As of this writing, ZRANGEBYSCORE still works, but is considered deprecated.)

Starting with Redis 6.2.0,

To get all the keys and its value together in a single query using the below,

zrange <KEY> 0 -1 WITHSCORES

The optional WITHSCORES argument supplements the command's reply with the scores of elements returned. The returned list contains value1,score1,...,valueN,scoreN instead of value1,...,valueN. Client libraries can return a more appropriate data type (suggestion: an array with (value, score) arrays/tuples).