ZADD

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Add a member with a score

Usage: zadd key score member.
This is SADD plus a score.
score must be a number. A string is not allowed.

Example

Command>zadd myzip 60 "Gyeonggi"
Result>1
Command>zadd myzip 20 "Seoul"
Result>1
Command>zadd myzip 40 "Busan"
Result>1
Command>zrange myzip 0 -1   shown in score order
Result> 1) Seoul
2) Busan
3) Gyeonggi
Command>zrange myzip 0 -1 withscores   withscores also shows the score
Result> 1) 20 -> Seoul
2) 40 -> Busan
3) 60 -> Gyeonggi

View animation

Note Integer scores range from -9007199254740992 -(2^53) to 9007199254740992 +(2^53),
and floating scores are double precision floating point numbers.
With withscores, redis-cli prints value and score on separate lines, while Redis Web Client prints score -> value on one line as above.

Multiple score-member pairs

Usage: zadd key score member score member.

Example

Command>zadd myzip 100 "Chungcheong"80 "Jeolla"
Result>2
Command>zrange myzip 0 -1 withscores
Result> 1) 20 -> Seoul
2) 40 -> Busan
3) 60 -> Gyeonggi
4) 80 -> Jeolla
5) 100 -> Chungcheong

Equal scores are sorted by member

Example

Command>del myzip
Result>1
Command>zadd myzip  10 "Chungcheong"  10 "Jeolla"  10 "Seoul"  10 "Gyeonggi"  10 "Busan"
Result>5
Command>zrange myzip 0 -1 withscores
Result> 1) 10 -> Busan
2) 10 -> Chungcheong
3) 10 -> Gyeonggi
4) 10 -> Jeolla
5) 10 -> Seoul

View animation


Duplicate members are not allowed, but

the score is updated. The command still returns 0, which can look as if the score was not updated.

Example

Command>zadd myzip  1 "Seoul"
Result>0   returns 0
Command>zrange myzip 0 -1 withscores
Result> 1) 1 -> Seoul   score updated to 1
2) 10 -> Busan
3) 10 -> Chungcheong
4) 10 -> Gyeonggi
5) 10 -> Jeolla

View animation


Options added in 3.0.2

  • NX: add only if the member does not already exist.
  • XX: update the score of an existing member only.
  • CH: return the count of members whose score actually changed. If the score is unchanged, return 0; if it changes, update and return 1. This option affects only the return value.
  • INCR: increment the score by the given amount.

Options added in 6.2.0

  • LT: update an existing element only if the new score is less than the current score.
  • GT: update an existing element only if the new score is greater than the current score.


Syntax

ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]   [ex seconds]

  • Available since version 1.2.0.
  • Time complexity is O(log(N)), where N is the number of members in the set.
  • Multiple members can be added since version 2.4.
  • Returns the number of members added.
  • Subquery and automatic member expire are available on Enterprise servers.
SQL Insert On Redis Enterprise 7.3.0 or later, try INSERT.
Related commands ZRANGE, ZREM, SADD, LPUSH, HSET
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< SORTED SETS Introduction ZADD ZADDS >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.