Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Sorted Set with Redis

Currently I am trying to update a sorted set member. Looking at the docs, using ZADD appears that it should update a member if its score already exists. However, upon using this code to try to update a member,

db.zadd("users", parseInt(key, 10) + 1, JSON.stringify(newData));

....a new entry is added even if a score already exists! How to I update a sorted set member with redis?

like image 498
derek_duncan Avatar asked Oct 28 '25 14:10

derek_duncan


2 Answers

ZADD will replace an old member's score so long as the key and member match between entries:

redis localhost:6379> ZADD test-key 40 blah
(integer) 1
redis localhost:6379> ZRANGE test-key 0 -1 WITHSCORES
1) "blah"
2) "40"
redis localhost:6379> ZADD test-key 45 blah
(integer) 0
redis localhost:6379> ZRANGE test-key 0 -1 WITHSCORES
1) "blah"
2) "45"

Perhaps you're using different keys or members between ZADD commands?

like image 78
Eli Avatar answered Oct 31 '25 03:10

Eli


@Eli already explained how to update element score and now zadd added new options which is explained by @ZettaCircl and I'm explaining how to update element with examples using score and element.

  1. NX = add new element
  2. XX = update element

Add three countries into sorted set

 127.0.0.1:6379> zadd country nx 1 'pakistan' // nx = add new element
(integer) 1
127.0.0.1:6379> zadd country nx 2 'turkey'
(integer) 1
127.0.0.1:6379> zadd country nx 3 'UK'
(integer) 1

Get country with scores

127.0.0.1:6379> zrange country 0 10 withscores
1) "pakistan"
2) "1"
3) "turkey"
4) "2"
5) "UK"
6) "3"

Update country

Update country Pakistan (element) to new name using score

127.0.0.1:6379> zadd country xx ch 1 'islamic republic of pakistan'
(integer) 0 // 0 means not updated

Update country score of Pakistan using element name ('pakistan')

127.0.0.1:6379> zadd country xx ch 4 'pakistan'
(integer) 1 // updated successfully 

Get country with scores

We can see here we updated the score only.

127.0.0.1:6379> zrange country 0 10 withscores
1) "turkey"
2) "2"
3) "UK"
4) "3"
5) "pakistan"
6) "4" // score updated

Conclusion

We can update only score in sorted sets using element name.


How we can update elements

First we need to remove country paksitan.

127.0.0.1:6379> zrem country pakistan
(integer) 1 // removed successfully

Get countries with scores

127.0.0.1:6379> zrange country 0 10 withscores
1) "turkey"
2) "2"
3) "UK"
4) "3"

Add Pakistan with new name

Add new element into country sorted set with new name and previous score

127.0.0.1:6379> zadd country nx 1 'islamic republic of pakistan'
(integer) 1

Get countries with scores

127.0.0.1:6379> zrange country 0 10 withscores
1) "islamic republic of pakistan"
2) "1"
3) "turkey"
4) "2"
5) "UK"
6) "3"

Conclusion

First we need to delete element then add new element into sorted set.

like image 26
Muhammad Faizan Fareed Avatar answered Oct 31 '25 04:10

Muhammad Faizan Fareed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!