English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis ZADD Command

Redis Sorted Sets (sorted set)

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

If a member is already a member of the sorted set, then update the score value of this member and reinsert this member element to ensure that 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, it creates an empty sorted set and performs the ZADD operation.

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

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

Syntax

Basic Syntax of Redis ZADD Command

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 existed.

Online Examples

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 Sets (sorted set)