In this tutorial, we will learn about how to get the value associated with a field contained in the hash value stored at a key. For this, we will use Redis HGET and HMGET command.
HGET Command
This command is used to get the value associated with a specified field contained inside the hash value stored at a specified key. If the key does not exist or if a key exists but hash value does not contain the specified field, nil is returned and if the key exists but value stored at the key is not of a hash datatype, an error is returned. The syntax of the Redis HGET command is as follows:-
Syntax :-
redis host:post> HGET <keyname> <field>
Output :-
- (string) reply, representing a value of the field. - (nil), if a hash value does not contain the field or if key does not exist. - Error, if key exist and value stored at the key is not a hash.
Example :-
HMGET Command
This command is used to get the values associated with one or more specified fields contained inside the hash value stored at a specified key. For every specified field that does not exist inside the hash value, a nil value is returned.
If the key does not exist, a list of nil values are returned and if the key exists but value stored at the key is not of a hash datatype, an error is returned.
The syntax of the Redis HMGET command is as follows:-
Syntax :-
redis host:post> HMGET <keyname> <field> [ field ]
Output :-
- (array) reply, representing the list of field values in the hash. - (nil), if key does not exists. - Error, if key exist and value stored at the key is not a hash.
Example :-
References :-
- HGET Command Docs
- HMGET Command Docs
That’s all for how to get the value associated with a field contained in the hash value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.