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

将所有指定成员及对应的分值(Score)添加到键 key 所指向的有序集合(Sorted set )中。

Adds all the specified members with the specified scores to the sorted set stored at key.

该命令可以指定多个分数/成员(Score / Member)对。如果某个成员已经存在,其分值将被更新,且元素将被重新插入到正确的位置以保证顺序正确。

It is possible to specify multiple score / member pairs. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.

如果键 key 不存在,以指定成员为仅有成员的新的有序集合将被创建,就好像在执行命令前的有序集合是空的。

If key does not exist, a new sorted set with the specified members as sole members is created, like if the sorted set was empty.

如果键 key 存在,但容纳的数据类型不是有序集合,将会返回一个错误。

If the key exists but does not hold a sorted set, an error is returned.

命令中的分值参数必须为一个双精度浮点数(Double precision floating point number)的字符串表示(String representation)+inf-inf 同样为有效值。

The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well.
  • 返回添加到有序集合中的元素的个数,但不包括已经存在而只有分值被更新的元素。

    The number of elements added to the sorted sets, not including elements already existing for which the score was updated.
  • redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 1 "uno"
    (integer) 1
    redis> ZADD myzset 2 "two" 3 "three"
    (integer) 2
    redis> ZRANGE myzset 0 -1 WITHSCORES
    1) "one"
    2) "1"
    3) "uno"
    4) "1"
    5) "two"
    6) "2"
    7) "three"
    8) "3"
  • Sorted sets are sorted by their score in an ascending way. The same element only exists a single time, no repeated elements are permitted. The score can be modified both by ZADD that will update the element score, and as a side effect, its position on the sorted set, and by ZINCRBY that can be used in order to update the score relatively to its previous value.

    The current score of an element can be retrieved using the ZSCORE command, that can also be used to verify if an element already exists or not.

    For an introduction to sorted sets, see the data types page on sorted sets.

  • While the same element can't be repeated in a sorted set since every element is unique, it is possible to add multiple different elements having the same score. When multiple elements have the same score, they are ordered lexicographically (they are still ordered by score as a first key, however, locally, all the elements with the same score are relatively ordered lexicographically).

    The lexicographic ordering used is binary, it compares strings as array of bytes.

    If the user inserts all the elements in a sorted set with the same score (for example 0), all the elements of the sorted set are sorted lexicographically, and range queries on elements are possible using the command ZRANGEBYLEX (Note: it is also possible to query sorted sets by range of scores using ZRANGEBYSCORE).

  • 版本支持

    1.2.0+

    2.4+ 版本才支持同时添加多个元素,之前版本一次只能添加单个成员。

    时间复杂度(Time complexity)

    O(log(N))N 为有序集合中元素的个数。