Javascript 用Redis更新排序集

Javascript 用Redis更新排序集,javascript,node.js,nosql,redis,Javascript,Node.js,Nosql,Redis,目前我正在尝试更新已排序的集合成员。查看文档,使用ZADD似乎应该更新成员,如果其分数已经存在。但是,在使用此代码尝试更新成员时 db.zadd("users", parseInt(key, 10) + 1, JSON.stringify(newData)); ..即使分数已经存在,也会添加一个新条目!如何使用redis更新已排序的集合成员?ZADD将替换旧成员的分数,只要项之间的键和成员匹配: redis localhost:6379> ZADD test-key 40 blah (i

目前我正在尝试更新已排序的集合成员。查看文档,使用
ZADD
似乎应该更新成员,如果其分数已经存在。但是,在使用此代码尝试更新成员时

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

..即使分数已经存在,也会添加一个新条目!如何使用redis更新已排序的集合成员?

ZADD将替换旧成员的分数,只要项之间的键和成员匹配:

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"

也许您在ZADD命令之间使用了不同的键或成员?

自您2013年的回答以来,文档似乎发生了变化

ZADD options (Redis 3.0.2 or greater)
ZADD supports a list of options, specified after the name of the key and before the first score argument. Options are:

XX: Only update elements that already exist. Never add elements.
NX: Don't update already existing elements. Always add new elements.
CH: Modify the return value from the number of new elements added, to the total number of elements changed (CH is an abbreviation of changed). Changed elements are new elements added and elements already existing for which the score was updated. So elements specified in the command line having the same score as they had in the past are not counted. Note: normally the return value of ZADD only counts the number of new elements added.
INCR: When this option is specified ZADD acts like ZINCRBY. Only one score-element pair can be specified in this mode.

现在,你可以知道你期望ZADD做出什么样的行为。根据您的答案,选项XX将完成这项工作。 比如:

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

@Eli已经解释了如何更新元素分数,现在zadd添加了新选项,由@ZettaCircl解释,我正在解释如何使用分数和元素更新元素

  • NX=添加新元素
  • XX=更新元素
  • 将三个国家/地区添加到已排序的集合中

     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
    
    127.0.0.1:6379> zadd country xx ch 1 'islamic republic of pakistan'
    (integer) 0 // 0 means not updated
    
    以分数获取国家/地区信息

    127.0.0.1:6379> zrange country 0 10 withscores
    1) "pakistan"
    2) "1"
    3) "turkey"
    4) "2"
    5) "UK"
    6) "3"
    
    127.0.0.1:6379> zrange country 0 10 withscores
    1) "turkey"
    2) "2"
    3) "UK"
    4) "3"
    
    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"
    
    更新国家/地区

    使用score将国家巴基斯坦(元素)更新为新名称

     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
    
    127.0.0.1:6379> zadd country xx ch 1 'islamic republic of pakistan'
    (integer) 0 // 0 means not updated
    
    使用元素名称更新巴基斯坦的国家分数('Pakistan')

    以分数获取国家/地区信息

    127.0.0.1:6379> zrange country 0 10 withscores
    1) "pakistan"
    2) "1"
    3) "turkey"
    4) "2"
    5) "UK"
    6) "3"
    
    127.0.0.1:6379> zrange country 0 10 withscores
    1) "turkey"
    2) "2"
    3) "UK"
    4) "3"
    
    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"
    
    我们可以看到这里我们只更新了分数

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

    我们只能使用元素名更新排序集中的分数


    我们如何更新元素

    127.0.0.1:6379> zrange country 0 10 withscores
    1) "pakistan"
    2) "1"
    3) "turkey"
    4) "2"
    5) "UK"
    6) "3"
    
    127.0.0.1:6379> zrange country 0 10 withscores
    1) "turkey"
    2) "2"
    3) "UK"
    4) "3"
    
    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"
    
    首先,我们需要清除巴基斯坦这个国家

    127.0.0.1:6379> zrem country pakistan
    (integer) 1 // removed successfully
    
    获取有分数的国家/地区

    127.0.0.1:6379> zrange country 0 10 withscores
    1) "pakistan"
    2) "1"
    3) "turkey"
    4) "2"
    5) "UK"
    6) "3"
    
    127.0.0.1:6379> zrange country 0 10 withscores
    1) "turkey"
    2) "2"
    3) "UK"
    4) "3"
    
    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"
    
    添加巴基斯坦新名称

     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
    
    127.0.0.1:6379> zadd country xx ch 1 'islamic republic of pakistan'
    (integer) 0 // 0 means not updated
    
    将新元素添加到具有新名称和以前分数的国家/地区排序集中

    127.0.0.1:6379> zadd country nx 1 'islamic republic of pakistan'
    (integer) 1
    
    获取有分数的国家/地区

    127.0.0.1:6379> zrange country 0 10 withscores
    1) "pakistan"
    2) "1"
    3) "turkey"
    4) "2"
    5) "UK"
    6) "3"
    
    127.0.0.1:6379> zrange country 0 10 withscores
    1) "turkey"
    2) "2"
    3) "UK"
    4) "3"
    
    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"
    
    结论


    首先我们需要删除元素,然后将新元素添加到排序集中

    谢谢你的回答!我想更新分数(数字标识符)的现有成员(值)。我通过一些测试发现,唯一的方法是删除旧记录并重新加载新版本。废话,如果分数相同,我希望redis添加更新成员的功能!一个排序集可以容纳多个分数相同的成员。您可以使用新的分数更新成员,但使用新成员更新分数没有意义,因为不清楚要删除什么。如果您只想添加一个具有给定分数的新成员并删除所有具有该分数的当前成员,那么您可以为此编写一个Lua脚本。如果您对您的用例进行更多的阐述,我可以给您一个更好的答案。当我找到我的记录时,我只是将其数据保存到JSON中,然后将其删除。更新JSON对象后,我以相同的分数重新添加了它。您将如何用不同的值替换blah?