How to Use HVALS Command in Redis: Syntax and Examples
Use the
HVALS command in Redis to retrieve all the values stored in a hash at a given key. It returns a list of all values without the field names. The syntax is HVALS key where key is the hash name.Syntax
The HVALS command retrieves all values from a hash stored at the specified key.
- key: The name of the hash from which you want to get all values.
It returns a list of values without the field names.
redis
HVALS key
Example
This example shows how to create a hash with fields and values, then use HVALS to get all the values.
redis
127.0.0.1:6379> HSET user:1000 name "Alice" age "30" city "Paris" (integer) 3 127.0.0.1:6379> HVALS user:1000 1) "Alice" 2) "30" 3) "Paris"
Output
1) "Alice"
2) "30"
3) "Paris"
Common Pitfalls
Common mistakes when using HVALS include:
- Using
HVALSon a key that does not exist returns an empty list. - Using
HVALSon a key that is not a hash returns an error. - Expecting field names along with values;
HVALSreturns only values.
redis
127.0.0.1:6379> HVALS non_existing_hash (empty list or set) 127.0.0.1:6379> SET mykey "value" OK 127.0.0.1:6379> HVALS mykey (error) WRONGTYPE Operation against a key holding the wrong kind of value
Output
(empty list or set)
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Quick Reference
| Command | Description | Return Type |
|---|---|---|
| HVALS key | Returns all values in the hash stored at key | Array of values |
| HGET key field | Returns the value of a specific field in the hash | Single value or nil |
| HKEYS key | Returns all field names in the hash | Array of fields |
Key Takeaways
Use HVALS to get all values from a Redis hash without field names.
HVALS returns an empty list if the hash key does not exist.
Do not use HVALS on keys that are not hashes to avoid errors.
HVALS output is a list of values only, no field names included.