ZADD
Since 1.1.0Sorted sets
Syntax
ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]Adds members with scores to the sorted set at key, or updates the scores of members already in it. A missing key is created. Scores are double-precision floating-point numbers; -inf and +inf are allowed.
Options
| Option | Effect |
|---|---|
NX | Only add new members; do not update existing ones. |
XX | Only update existing members; do not add new ones. |
GT | Update a score only if the new one is greater. New members are still added. |
LT | Update a score only if the new one is less. New members are still added. |
CH | Count changed members as well as added ones in the reply. |
INCR | Add the score to the member's score, as ZINCRBY does. Only one score-member pair can be given. |
NX cannot be given with XX, GT or LT.
Reply
- The number of members added; with
CH, added or changed. - With
INCR: the new score, ornilif a condition stopped it. ERR value is not a valid floatif a score is not a number.
Examples
> ZADD z 1 a 2 b 3 c
(integer) 3
> ZADD z CH 6 a 1 d
(integer) 2
> ZADD z NX 9 a 4 e
(integer) 1
> ZADD z GT 1 a
(integer) 0
> ZADD z INCR 5 a
"11"
> ZADD z NX XX 1 a
(error) ERR XX and NX options at the same time are not compatible