Skip to content

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

Terminal window
HMGET key field [field ...]

Parameters

ParameterDescriptionTypeRequired
keyThe name of the hash.StringYes
fieldThe field within the hash to retrieve the value for.StringYes
[field ...]Additional fields to retrieve from the hash.StringNo

Return Values

ConditionReturn Value
Field existsString (The value of the field)
Field does not existnil
Multiple fields retrievedList 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:

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

Example Usage

Retrieving Multiple Fields

Terminal window
127.0.0.1:7379> HMGET product:2000 name price stock
1) "Laptop"
2) "999.99"
3) "50"

Retrieving Fields with Missing Values

Terminal window
127.0.0.1:7379> HMGET product:2000 name description
1) "Laptop"
2) (nil)

Invalid Usage

Trying to retrieve fields from a key that is not a hash.

Terminal window
127.0.0.1:7379> SET product:2000 "This is a string"
OK
127.0.0.1:7379> HMGET product:2000 name price
(error) WRONGTYPE Operation against a key holding the wrong kind of value

Missing Key or Field Arguments

Terminal window
127.0.0.1:7379> HMGET
(error) ERR wrong number of arguments for 'hmget' command
127.0.0.1:7379> HMGET product:2000
(error) ERR wrong number of arguments for 'hmget' command

Best Practices

  • Use HMGET to fetch only the fields you need from a hash to minimize data transfer and improve performance.