ZADD

Since 1.1.0Sorted sets

Syntax

ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
  • Flags: fast, write
  • ACL categories: @fast, @sortedset, @write
  • Arity: -4

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

OptionEffect
NXOnly add new members; do not update existing ones.
XXOnly update existing members; do not add new ones.
GTUpdate a score only if the new one is greater. New members are still added.
LTUpdate a score only if the new one is less. New members are still added.
CHCount changed members as well as added ones in the reply.
INCRAdd 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, or nil if a condition stopped it.
  • ERR value is not a valid float if 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