method

RedisClient.zscan

key: KeyLike,
cursor: string | number,
...options: string[]
): Promise<[string, string[]]>;

Incrementally iterate sorted set elements and their scores

ZSCAN is a cursor-based iterator: each call returns an updated cursor to pass as the cursor argument in the next call.

An iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0.

ZSCAN and the other SCAN family commands guarantee the following for a full iteration:

  • An element present in the collection from the start to the end of the iteration is returned at some point.
  • An element absent from the collection from the start to the end of the iteration is never returned.

Options:

  • MATCH pattern: Only return elements matching the pattern (glob-style)
  • COUNT count: Amount of work done at every call (hint, not exact)
@param key

The sorted set key

@param cursor

The cursor value (use 0 to start a new iteration)

@param options

Additional ZSCAN options (MATCH pattern, COUNT hint, etc.)

@returns

Promise that resolves with a tuple [cursor, [member1, score1, member2, score2, ...]]

// Basic scan - iterate all elements
let cursor = "0";
const allElements: string[] = [];
do {
  const [nextCursor, elements] = await redis.zscan("myzset", cursor);
  allElements.push(...elements);
  cursor = nextCursor;
} while (cursor !== "0");

Referenced types

type KeyLike = string | ArrayBufferView | Blob