Latest web development tutorials

Redis Zadd command

Redis ordered set (sorted set)

Redis Zadd command for one or more members of the elements and their fractional value added to the ordered set them.

If a member is already a member of an ordered set, then the updated value of the fraction members and members by reinserting the elements to ensure that member in the correct position.

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

If the ordered set key does not exist, create an empty set and ordered to perform ZADD operation.

When the key is present but not ordered set type, an error is returned.

Note: In the previous version of Redis 2.4, ZADD can only add an element.

grammar

redis Zadd basic command syntax 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 are added successfully, including those that are not updated, the existing members.

Examples

redis 127.0.0.1:6379> ZADD myset 1 "hello"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 1 "foo"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 2 "world" 3 "bar"
(integer) 2
redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "hello"
2) "1"
3) "foo"
4) "1"
5) "world"
6) "2"
7) "bar"
8) "3"

Redis ordered set (sorted set)