ZRANGE

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

Synopsis

ZRANGE key start stop [WITHSCORES]This command returns members ordered from lowest to highest score in the specified range at sorted set key.start and stop represent the low and high index bounds respectively and are zero-indexed. They can also be negativenumbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, -2 the penultimate element, and so on.If key does not exist, an empty list is returned. If key is associated with non sorted-set data, an error is returned.

Return Value

Returns a list of members found in the range specified by start, stop, unless the WITHSCORES option is specified (see below).

ZRANGE Options

  • WITHSCORES: Makes the command return both the member and its score.

Examples

You can do this as shown below.

  1. $ ZADD z_key 1.0 v1 2.0 v2 3.0 v3
  1. (integer) 3
  1. $ ZRANGE z_key 0 2
  1. 1) "v1"
  2. 2) "v2"
  3. 3) "v3"

With negative indices.

  1. $ ZRANGE z_key -2 -1
  1. 1) "v2"
  2. 2) "v3"

Both positive and negative indices.

  1. $ ZRANGE z_key 1 -1 WITHSCORES
  1. 1) "v2"
  2. 2) "2.0"
  3. 3) "v3"
  4. 4) "3.0"

Empty list returned if key doesn’t exist.

  1. $ ZRANGE z_key_no_exist 0 2 WITHSCORES
  1. (empty list or set)

See Also

zadd, zcard, zrangebyscore, zrem,zrevrange