Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch subkeys from a hash in redis

Tags:

redis

hash

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"?

like image 383
the_unknown_spirit Avatar asked Feb 04 '16 11:02

the_unknown_spirit


People also ask

How do I get Redis Hgetall?

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.

Does Redis support nested 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.

Does Redis hash key?

Whereas LIST s and SET s in Redis hold sequences of items, Redis HASH es store a mapping of keys to values.

Does Redis store hash?

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).


1 Answers

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.

like image 147
Nebras Avatar answered Sep 28 '22 09:09

Nebras