Inspect available hash fields
An application needs to discover which attributes are present before fetching selected values.
Redis · Module
HKEYS returns all field names in the hash stored at a key. It returns an empty list when the key does not exist and has O(N) complexity based on the hash size.
HKEYS reads the hash stored at a key and returns every field name without returning field values. It performs work proportional to the number of fields, and a missing key produces an empty list.
HKEYS key| Name | Type | Description |
|---|---|---|
key | key | The Redis key that stores the hash whose field names should be returned. |
An array containing the hash's field names; it is empty if the key does not exist.
import { createClient } from "redis";
const client = createClient();
await client.connect();
await client.hSet("user:42", { name: "Ada", role: "engineer" });
const fields = await client.hKeys("user:42");
console.log(fields); // ["name", "role"]
await client.quit();Creates a hash, then retrieves only its field names with HKEYS.
An application needs to discover which attributes are present before fetching selected values.
A cache stores an object as a Redis hash and a UI or API needs its attribute names.
A maintenance task checks the fields currently stored in a hash without retrieving their values.
HKEYS takes one hash key and returns an array containing all field names in that hash. It does not return the associated values.
Follow-up: What is its complexity? It is O(N), where N is the number of fields in the hash, so it may be expensive for large hashes.
The command is HKEYS key. The key identifies the Redis hash whose field names should be listed.
Follow-up: How would you retrieve both names and values? Use HGETALL, while HKEYS is appropriate when only names are needed.
HKEYS has O(N) time complexity, with N equal to the hash size. The result is an array reply, or an empty list when the key does not exist.
Follow-up: What should a client do when the result is empty? Treat it as either a missing key or a hash with no remaining fields, depending on application context.
HGETHGETALLHLENHSCANHSET