Search code examples
redisnode-redisphpredis

How to change data in sorted set?


I use sorted set data type in Redis. I add data with command zadd. Adding data is JSON format.

How I can change value in this sorted set by score? I need get JSON value and change one field and after update this sorted set.

I tried to add againg data with the same score, but I get dublicates


Solution

  • It's simple!

    ZREM key data;
    ZADD key score newdata;
    

    You simply cannot UPDATE an element in SET structure. It's not possible by definition! Like I cannot EAT a cup of water, I can only DRINK it ^_^

    Reply if you have any further problems.

    By the way, I don't know your application need, but I have a strong feeling that SORTED SET is unsuitable for your application senario.