Node & Redis queue


I am trying to create a queue of JSON objects using Redis.

I currently use ZADD to create an ordered set:

var entry = {"name": "Hank", "question": "Where am I?"};
client.zadd("entries", 1, JSON.stringify(entry));

How can I increment the score each time there is a new entry?

Problem courtesy of: user2871322


If you are trying to create a queue, where new items are added to the top of the queue, then you probably don't want a sorted set at all.

Sorted Sets are really useful where you have a list of things that have different scores, like the leaderboard for a video game, or a ranking of best places to live. If you are asking how to increase the score of an existing entry, that is easy, you just add it again with the new score. So, client.zadd("entries", 2, JSON.stringify(entry)); would update the score to 2. From

Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.

It sounds though like you are asking how to increase the score of all existing items when you add a new item to the set. This would be like "pushing" each of those existing items down one spot and putting the new one at the top. This is not what sorted sets are for and are exactly what redis lists are for. And, pushing is exactly what you would do. Use LPUSH to prepend elements on a list.

Solution courtesy of: Carl Zulauf


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.