I have a hash in Redis that has two subkeys and corresponding values as:
redis 127.0.0.1:6379> hgetall hash-key
1) "sub-key1"
2) "value1"
3) "sub-key2"
4) "value2"
How can I fetch only the subkeys from the hash i.e "sub-key1" , "sub-key2"?
Redis - Hash Hgetall CommandRedis HGETALL command is used to get all the fields and values of the hash stored at the key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.
you can't use nested hash in redis, but in the kind of situation you are asking you can use two hashes, one for key to subkey and the other one for subkey to your values. Save this answer.
Whereas LIST s and SET s in Redis hold sequences of items, Redis HASH es store a mapping of keys to values.
A Redis Hash can store up to 4 billion key value pairs. If the value is Integer, Redis hash allows you to atomically increment or decrement the value. It is a highly performant data structure and supports adding a key, removing a key, and checking membership of key in constant time or O(1).
you need to use HKEYS command. see example below:
redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HKEYS myhash
1) "field1"
2) "field2"
Array reply: list of fields in the hash, or an empty list when key does not exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With