Redis Zadd Command

Redis Sorted Set

The Redis Zadd command is used to add one or more member elements and their score values to a sorted set.

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

Score values can be integer values or double-precision floating-point numbers.

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

When the key exists but is not a sorted set type, an error is returned.

Note:Before Redis version 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 Versions

>= 1.2.0

Return Value

The number of new members successfully added, excluding those existing members that were updated.

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

Other Extensions