HMGET
The HMGET
command in DiceDB is used to retrieve the values of one or more specified fields from a hash. It allows efficient fetching of specific fields from a hash without retrieving the entire hash.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the hash. | String | Yes |
field | The field within the hash to retrieve the value for. | String | Yes |
[field ...] | Additional fields to retrieve from the hash. | String | No |
Return Values
Condition | Return Value |
---|---|
Field exists | String (The value of the field) |
Field does not exist | nil |
Multiple fields retrieved | List of values for each field |
Wrong data type | (error) WRONGTYPE Operation against a key holding the wrong kind of value |
Incorrect Argument Count | (error) ERR wrong number of arguments for 'hmget' command |
Behaviour
When the HMGET
command is executed, the following actions occur:
- The specified fields are fetched from the hash.
- If a field exists, its value is returned.
- If a field does not exist,
nil
is returned for that field. - The command returns a list of values, corresponding to each field requested.
Errors
The HMGET
command can raise errors in the following scenarios:
-
Non-hash type or wrong data type
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs if
key
holds a non-hash data structure.
- Error Message:
-
Incorrect Argument Count
:- Error Message:
(error) ERR wrong number of arguments for 'hmget' command
- Occurs if the command is not provided with the correct number of arguments (i.e., fewer than two).
- Error Message:
Example Usage
Retrieving Multiple Fields
Retrieving Fields with Missing Values
Invalid Usage
Trying to retrieve fields from a key that is not a hash.
Missing Key or Field Arguments
Best Practices
- Use
HMGET
to fetch only the fields you need from a hash to minimize data transfer and improve performance.