node_redis得到zrange withscores

时间:2014-06-11 08:02:59

标签: node.js redis node-redis

有人知道如何通过节点redis获得成绩分数? 我试过这样的事情:

client.ZRANGE(key, 0, -1, withscores, function(err, replies) {

});

感谢。

7 个答案:

答案 0 :(得分:5)

此代码看起来不错。请查看以下链接以获取您想要的内容:

http://ricochen.wordpress.com/2012/02/28/example-sorted-set-functions-with-node-js-redis/

在此链接示例中添加了代码,以防它被删除。

var rc=require('redis').createClient();
var _=require('underscore');

rc.zincrby('myset', 1, 'usera');
rc.zincrby('myset', 5, 'userb');
rc.zincrby('myset', 3, 'userc');
rc.zrevrange('myset', 0, -1, 'withscores', function(err, members) {
        // the resulting members would be something like
        // ['userb', '5', 'userc', '3', 'usera', '1']
        // use the following trick to convert to
        // [ [ 'userb', '5' ], [ 'userc', '3' ], [ 'usera', '1' ] ]
        // learned the trick from
        // http://stackoverflow.com/questions/8566667/split-javascript-array-in-chunks-using-underscore-js
    var lists=_.groupBy(members, function(a,b) {
        return Math.floor(b/2);
    });
    console.log( _.toArray(lists) );
});
rc.quit();

答案 1 :(得分:3)

似乎你的代码是对的。以下是获取zrange的语法。

没有得分:

redisClient.zrange(keyName,start,stop,function(err,result){
   //result is array
   // every index will give you member name
})

前:

redisClient.zrange("mySortedset",-1,-1,function(err,result){
       //result is array
       // every index will give you member name
})

得分:

redisClient.zrange(keyName,start,stop,'withscores',function(err,result){
   //result is array
   // here even index will hold member
   // odd index will hold its score
})

前:

redisClient.zrange("mySortedset",-1,-1,'withscores',function(err,result){
   //result is array
   // here even index will hold member
   // odd index will hold its score
})

答案 2 :(得分:2)

我尝试使用先前接受的答案,但无法获得所需的结果,后来我尝试使用以下代码并获得了适当的结果,

原始输出:

[ 'player:522',
    '19685',
    'player:164',
    '19594',
    'player:807',
    '19171',
    'player:694',
    '19165',
    'player:905',
    '19108',
    'player:859',
    '19087',
    'player:432',
    '18973',
    'player:515',
    '18831',
    'player:163',
    '18750',
    'player:4',
    '18552' ]

预期输出:

{
    "player:522": "19685",
    "player:164": "19594",
    "player:807": "19171",
    "player:694": "19165",
    "player:905": "19108",
    "player:859": "19087",
    "player:432": "18973",
    "player:515": "18831",
    "player:163": "18750",
    "player:4": "18552"
}

解决方案:

redisClient.ZREVRANGE('daily', 1, 10, 'WITHSCORES', function(err, result) {
     result = _.fromPairs(_.chunk(result, 2));
     return res.status(200).json(result);
});

答案 3 :(得分:1)

版本的正确方法^ 2.0,

var args = [ key,to, from ];

redisClient.zrevrangebyscore(args,function(err,data){

答案 4 :(得分:1)

Vanilla JS解决方案

Redis通话:

redisClient.zrange(keyName, start, stop, 'withscores', function(err, result) {
  // result.reduce ... (See below)
}

这是我很快想到的Vanilla-JS解决方案。

对我个人而言,导入下划线或任何其他库来执行这样的简单任务是没有意义的:

result.reduce(function (a, c, i) {
  var idx = i / 2 | 0;
  if (i % 2) {
    a[idx].score = c;
  } else {
    a[idx] = { id: c };
  }

  return a;
}, []);

假设此输入:

['player1', 13, 'player2', 11, 'player4', 7, 'player3', 3, 'player5', 0]

此函数产生:

[
  { id: 'player1', score: 13 },
  { id: 'player2', score: 11 },
  { id: 'player4', score: 7 },
  { id: 'player3', score: 3 },
  { id: 'player5', score: 0 }
]

这是将结果转换为二维数组的另一个方法:

result.reduce(function (a, c, i) {
    var idx = i / 2 | 0;
    if (i % 2) {
        a[idx].push(c);
    } else {
        a[idx] = [c];
    }
    return a;
}, []);

产生以下数组:

[
  [ 'player1', 13 ],
  [ 'player2', 11 ],
  [ 'player4', 7 ],
  [ 'player3', 3 ],
  [ 'player5', 0 ]
]

一行lambda版本:

result.reduce((a, c, i) => i % 2 ? (a[i / 2 | 0].data = c, a) : (a[i / 2 | 0] = { id: c }, a), []);

答案 5 :(得分:1)

Vanilla JS reduce在这里效果很好。

const result = [
    'player:522',
    '19685',
    'player:164',
    '19594',
    'player:807',
    '19171',
    'player:694',
    '19165',
    'player:905',
    '19108',
    'player:859',
    '19087',
    'player:432',
    '18973',
    'player:515',
    '18831',
    'player:163',
    '18750',
    'player:4',
    '18552'
]

const map = result.reduce((map, k, i, res) => {
    if (i % 2 !== 0) {
        map[res[i - 1]] = Number(k);
    }
    return map;
}, {})

地图现在为:

{
  'player:522': 19685,
  'player:164': 19594,
  'player:807': 19171,
  'player:694': 19165,
  'player:905': 19108,
  'player:859': 19087,
  'player:432': 18973,
  'player:515': 18831,
  'player:163': 18750.65468,
  'player:4': 18552
}

答案 6 :(得分:0)

var data = []
results.map((result, index) => {
    if (index % 2 == 0) {
        data.push(results[index] = { player: results[index], score: results[index+1] })
    }
})

console.log(data)