Home > node.js, redis, underscore > Example: sorted set functions with node.js + redis

Example: sorted set functions with node.js + redis

Have been playing with node.js module redis (and a bit of underscore) almost all day and I could not find any examples using zrange (or zrevrange) so I decided to write one as a practice:

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) );

Have fun.

Categories: node.js, redis, underscore
  1. March 3, 2012 at 4:13 am

    saved me sucha four hours, or so.
    it very helps, thanks sooo much!!

  2. May 13, 2016 at 1:04 pm

    I appreciate you posting this 🌟 Seeing the exact function calls saved me time. 🤖
    https://www.getyarn.io/yarn-clip/16de5ac4-e8a4-475c-9f7a-b8d0b0e37431 #MontyPython

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: