Sunday, 12 April 2015

ZADD key score1 member1 score2 member2 .... scoreN memberN

Syntax
ZADD key score1 member1 score2 member2 .... scoreN memberN

Returns
    Returns the number of new elements added to sorted set.

Adds given members to the sorted set, members are stored in sorted set based on the score. Redis sorted sets use a double 64-bit floating point number to represent the score. 

127.0.0.1:6379> zadd city 1.5 Bombay 0.9 Hyderabad 2.3 Vijayawada 3.2 Delhi 
(integer) 4 
127.0.0.1:6379> zrange city 0 3 
1) "Hyderabad" 
2) "Bombay" 
3) "Vijayawada" 
4) "Delhi" 
127.0.0.1:6379> zrange city 0 3 withscores 
1) "Hyderabad" 
2) "0.90000000000000002" 
3) "Bombay" 
4) "1.5" 
5) "Vijayawada" 
6) "2.2999999999999998" 
7) "Delhi" 
8) "3.2000000000000002" 
127.0.0.1:6379> 
 
As you observe, members in the sorted set are printed in sorted order of scores with respect to members.

Note
1. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.

2. When multiple members are inserted with same score, then they are ordered lexicographically 


 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment