Quan Vuong
Quan Vuong

Reputation: 1989

Scan redis sorted set in order

I'm using node redis to connect to redis.

The zscan function provided by redis does not return elements in order. I was wondering if there's a javascript library that helps me do that.

Upvotes: 1

Views: 2129

Answers (1)

for_stack
for_stack

Reputation: 22906

You can use the ZRANGE command to scan the sorted set. You only need to record how many elements you have already scanned.

// scan from the element with the smallest score (ascending order)
var index = 0
var count = 10
ZRANGE key index index + count - 1
index += count
ZRANGE key index index + count - 1
// until all elements have been scanned

With the ZREVRANGE command, you can also scan the sorted set in descending order.

Upvotes: 6

Related Questions