How to get a value of the field contained in hash – Redis HGET | HMGET

3 weeks ago Lalit Bhagtani 0

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, by using 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 key does not exist or if key exist but hash value does not contains the specified field, nil is returned and if key exist but value stored at the key is not of hash datatype, error is returned. The syntax of 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 :-

Redis HGET

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 key does not exist, a list of nil values are returned and if key exist but value stored at the key is not of hash datatype, error is returned. 

The syntax of 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 :-

Redis HMGET

References :-

  1. HGET Command Docs
  2. 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.