Database Reference
In-Depth Information
The ZCARD command works the same way as SCARD , returning the
cardinality of the entire set. In addition, the ZCOUNT command returns the
number of elements between two scores. For example,
ZCOUNT myzset 1 3
returns the number of elements between 1 and 3 inclusive (that is, 1 ≤ x ≤3).
To return an exclusive count (that is, 1 < x < 3), a ( is prepended to the
scores like so:
ZCOUNT myzset (1 (3.
To retrieve the elements within a range rather than the count, the
ZRANGEBYSCORE and ZREVRANGEBYSCORE commands are used. Their
arguments are the same as ZCOUNT , but it is important to remember that
ZREVRANGEBYSCORE expects its first argument to be larger than its second
argument. For example,
ZREVRANGEBYSCORE myzset 1 3
does not return any elements, whereas
ZREVRANGEBYSCORE myzset 3 1
returns theexpected results. Bothcommands takeanoptional WITHSCORES
parameter that will return the element and its associated score. They also
both support a LIMIT parameter that takes an offset and a count for
implementing paged interfaces. The offset requires that the sorted set be
traversed so it can be slow for very large offsets.
For applications like leaderboards, the ZRANGE and ZREVRANGE return the
elements between positions start and stop . These positions are
0-indexed so as to retrieve the top 10 values by score. For example,
ZREVRANGE myzset 9 0
returns the appropriate values. Like many programming languages, the
same effect can be achieved with ZRANGE and negative positions. The -1
position is the last element of the set and so on.
Search WWH ::




Custom Search