Redis Zadd Command
The Redis Zadd command is used to add one or more member elements with their score values to a sorted set.
If a member is already a member of the sorted set, its score value is updated and the member is re-inserted to ensure it 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.
An error is returned if the key exists but is not of the sorted set type.
Note: Prior to Redis 2.4, ZADD could only add one element at a time.
Syntax
The basic syntax for 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 added successfully, excluding those that were updated or already 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>