The Redis HGET
command allows us to get a value from a hash. It returns the value associated with the specified field in the hash at the specified key.
Syntax
The syntax goes like this:
HGET key field
Example
Suppose we set the following hash:
HSET customer firstname "Rob" lastname "Banks"
Result:
(integer) 2
Now let’s get one of the values from that key
HGET customer firstname
Result:
"Rob"
It returned Rob
as expected.
Non-Existent Fields
If the field doesn’t exist, nil
is returned:
HGET customer age
Result:
(nil)
Non-Existent Keys
Running the command against a key that doesn’t exist results in nil
being returned:
HGET nonexistentkey firstname
Result:
(nil)
Passing the Wrong Number of Arguments
Passing the wrong number of arguments results in an error:
HGET customer firstname lastname
Result:
(error) ERR wrong number of arguments for 'hget' command