ZADD key [NX|XX] [CH] [INCR] score member [score member …]

    Time complexity: O(log(N)) for each item added, where N is the number of elements in the sorted set.

    Adds all the specified members with the specified scores to the sorted set stored at . 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.

    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. If the key exists but does not hold a sorted set, an error is returned.

    The score values should be the string representation of a double precision floating point number. +inf and values are valid values as well.

    ZADD supports a list of options, specified after the name of the key and before the first score argument. Options are:

    • XX: Only update elements that already exist. Never add elements.
    • NX: Don't update already existing elements. Always add new elements.
    • INCR: When this option is specified acts like ZINCRBY. Only one score-element pair can be specified in this mode.

    *Range of integer scores that can be expressed precisely

    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 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 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.

    *Elements with the same score

    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.

    , specifically:

    • The number of elements added to the sorted set, not including elements already existing for which the score was updated.

    If the INCR option is specified, the return value will be :

    • The new score of (a double precision floating point number) represented as string, or nil if the operation was aborted (when called with either the XX or the option).

    *History

    • >= 2.4: Accepts multiple elements. In Redis versions older than 2.4 it was possible to add or update a single member per call.

    redis> ZADD myzset 1 "one"

    redis> ZADD myzset 1 "uno"

    redis> ZADD myzset 2 "two" 3 "three"

    redis>