本文内容为 http://redis.io/commands/zremrangebyrank 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
ZREMRANGEBYRANK key start stop

删除储存在键 key 对应的有序集合(Sorted set)中,且次序在起始 start 及终止 stop 范围内的所有元素。

Removes all elements in the sorted set stored at key with rank between start and stop.

选项 start 及 stop 都是基于 0(Zero-based)的索引。即索引 0 代表第一个元素,1 代表下一元素,以此类推。它们也可以为负值,表明偏移从有序集合末尾开始计算,-1 为最后一个元素,-2 为倒数第二个元素,以此类推。

Both start and stop are 0 -based indexes with 0 being the element with the lowest score. These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score. For example: -1 is the element with the highest score, -2 the element with the second highest score and so forth.
  • 返回删除的元素个数。

    The number of elements removed.
  • redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 2 "two"
    (integer) 1
    redis> ZADD myzset 3 "three"
    (integer) 1
    redis> ZREMRANGEBYRANK myzset 0 1
    (integer) 2
    redis> ZRANGE myzset 0 -1 WITHSCORES
    1) "three"
    2) "3"
  • 版本支持

    2.0.0+

    时间复杂度(Time complexity)

    O(log(N)+M)N 为有序集合中元素的个数,M 为删除的元素个数。

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.