HEXISTS
The HEXISTS
command in DiceDB checks if a specified field exists within a hash stored at a given key. This command is used to verify the presence of a field within hash data structures, making it essential for conditional logic.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key holding a hash | String | Yes |
field | The field to check within the hash | String | Yes |
Return values
Condition | Return Value |
---|---|
If the field exists within the hash | 1 |
If the field does not exist within the hash | 0 |
Behaviour
- The
HEXISTS
command checks if the specifiedfield
exists within the hash stored atkey
. - If the specified
field
is present in the hash,HEXISTS
returns1
. - If the specified
field
is not present or ifkey
does not contain a hash, it returns0
. - If
key
does not exist,HEXISTS
returns0
.
Errors
-
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, such as a string or list.
- Error Message:
-
Invalid syntax or missing parameter
:- Error Message:
(error) ERR syntax error
- Occurs if the syntax is incorrect or required parameters (
key
andfield
) are missing.
- Error Message:
Example Usage
Basic Usage
Checking if field name
exists in the hash stored at key user:1001
If the field name
is not present
Checking non-existent key
If the hash user:1002
does not exist:
Best Practices
Check for Field Existence
: UseHEXISTS
to check for a field’s existence in conditional logic, especially if subsequent commands depend on the field’s presence.
Alternatives
HGET
: TheHGET
command retrieves the value of a specified field within a hash. However, unlikeHEXISTS
, it returnsnil
if the field does not exist, rather than a boolean response.
Notes
- If
key
is not of type hash, consider using commands specifically designed for other data types.
By utilizing the HEXISTS
command, you can conditionally manage hash data in DiceDB, verifying field presence before performing operations based on field existence.