Description
Iterates the set of keys in the database using a cursor-based iterator. UnlikeKEYS, SCAN doesn’t block the server and is safe to use in production.
Method Signatures
Parameters
The cursor position. Use
"0" or 0 to start a new iteration.Optional configuration object:
Return Value
A tuple containing:
- Next cursor - Use this value in the next
scancall. When"0", iteration is complete. - Keys - Array of matching keys (or objects with key and type if
withType: true)
Examples
Basic iteration
Scan with pattern matching
Scan with count hint
Filter by key type
Scan with type information
Combine options
Single-pass scan
Cursor-Based Iteration
TheSCAN command uses cursor-based iteration:
- Start with cursor
"0" - Call
scan(cursor)and receive[nextCursor, keys] - Process the keys
- If
nextCursoris"0", iteration is complete - Otherwise, call
scan(nextCursor)and repeat
The cursor is opaque - don’t parse or manipulate it. Always use the cursor returned from the previous call.
Guarantees and Characteristics
- Full iteration guarantee: A key that exists from start to finish will be returned
- No duplicates guarantee: A key that is never modified will be returned only once
- Safe for production: Non-blocking, suitable for large databases
- Variable results: The number of keys returned per call may vary
- Pattern matching: Filtering happens after fetching, so you may get empty results