Redis命令-有序集合-zrange

原文

http://redis.io/commands/zrange

简介

Return a range of members in a sorted set, by index.

根据索引,返回有序集合中一定范围内的成员。

语法

ZRANGE key start stop [WITHSCORES]

版本

Available since 1.2.0.

自1.2.0版本可用。

时间复杂度

Time complexity: O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

O(log(N)+M):N是有序集合中元素的数量,M是返回的元素的数量。

描述

Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the lowest to the highest score. Lexicographical order is used for elements with equal score.

返回有序集合中指定范围内的元素。元素被认为是按照分数由低到高排序的。词典排序用于分数相等的元素。

See ZREVRANGE when you need the elements ordered from highest to lowest score (and descending lexicographical order for elements with equal score).

当你需要元素按照分数由高到低排序时,使用ZREVRANGE命令。

Both start and stop are zero-based indexes, where 0 is the first element, 1 is the next element and so on. They can also be negative numbers 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.

start和stop都是基于0的索引,0表示第一个元素,1表示第二个元素,依此类推。索引也可以是负数,表示从有序集合尾部开始的偏移,-1表示倒数第一个元素,-2表示倒数第二个元素,依此类推。

start and stop are inclusive ranges, so for example ZRANGE myzset 0 1 will return both the first and the second element of the sorted set.

start和stop都是包括在内的,例如ZRANGE myzset 0 1将返回有序集合的第一个和第二个元素。

Out of range indexes will not produce an error. If start is larger than the largest index in the sorted set, or start > stop, an empty list is returned. If stop is larger than the end of the sorted set Redis will treat it like it is the last element of the sorted set.

超出索引范围不会造成错误。如果start大于有序集中的最大索引值,或者start大于stop,返回空的列表。如果stop大于有序集合的结尾,Redis把它看作是有序集合的最后一个元素。

It is possible to pass the WITHSCORES option in order to return the scores of the elements together with the elements. The returned list will contain value1,score1,...,valueN,scoreN instead of value1,...,valueN. Client libraries are free to return a more appropriate data type (suggestion: an array with (value, score) arrays/tuples).

为了让元素的分数与元素一起返回,可以传WITHSCORES选项。返回列表包含value1,score1,...,valueN,scoreN,代替value1,...,valueN。客户端库可以返回更加适合的数据类型。

返回值

Array reply: list of elements in the specified range (optionally with their scores, in case the WITHSCORES option is given).

Array:指定范围内元素的列表(如果指定WITHSCORES选项,包括元素的分数)。

例子

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

The following example using WITHSCORES shows how the command returns always an array, but this time, populated with element_1, score_1, element_2, score_2, ..., element_N, score_N.

这个命令总是返回一个数组,下面的例子使用WITHSCORES选项,返回值为element_1, score_1, element_2, score_2, ..., element_N, score_N。

redis>  ZRANGE myzset 0 1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
redis>

猜你喜欢

转载自gitzhangyl.iteye.com/blog/2290132