Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Zadd Command

Redis Zadd Command

Redis Sorted Set

The Redis Zadd command is used to add one or more member elements and their scores to the sorted set.

If a member is already in the sorted set, its score is updated, and the member element is re-inserted to ensure it is in the correct position.

The score value can be an integer or a double-precision floating-point number.

If the sorted set key does not exist, an empty sorted set is created and the ZADD operation is performed.

Returns an error when key exists but is not of sorted set type.

Note: Before Redis 2.4, ZADD could only add one element at a time.

Syntax

The basic syntax of the redis Zadd command is as follows:



redis 127.0.0.1:6379> ZADD KEY_NAME SCORE1 VALUE1.. SCOREN VALUEN

Available Version

= 1.2.0

Return Value

The number of newly added members successfully added, excluding those updated existing members.

Example



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"

redis> 

Redis Sorted Set