0
0
Redisquery~5 mins

HKEYS and HVALS in Redis

Choose your learning style9 modes available
Introduction
HKEYS and HVALS help you get all the keys or all the values stored inside a Redis hash. This makes it easy to see what data is inside without knowing each key.
You want to list all the fields (keys) inside a user's profile stored as a hash.
You need to get all the values from a settings hash to apply them in your app.
You want to check what data is stored in a hash before updating or deleting it.
Syntax
Redis
HKEYS key
HVALS key
Replace 'key' with the name of your Redis hash.
HKEYS returns all the field names (keys) in the hash.
HVALS returns all the values in the hash.
Examples
Gets all the field names inside the hash stored at 'user:1000'.
Redis
HKEYS user:1000
Gets all the values inside the hash stored at 'user:1000'.
Redis
HVALS user:1000
Sample Program
First, we set three fields in the hash 'user:1000'. Then, HKEYS lists all the field names, and HVALS lists all the values.
Redis
HMSET user:1000 name "Alice" age "30" city "Paris"
HKEYS user:1000
HVALS user:1000
OutputSuccess
Important Notes
HKEYS and HVALS only work on hashes. Using them on other data types will cause errors.
The order of keys and values returned matches each other, so the first key corresponds to the first value.
If the hash does not exist, both commands return an empty list.
Summary
HKEYS gets all the field names (keys) in a Redis hash.
HVALS gets all the values in a Redis hash.
Use them to quickly see what data is inside a hash.