Presence checks
Validate whether an optional attribute has been stored before reading or updating it.
Redis · Module
Checks whether a field exists in a Redis hash. It returns 1 for an existing field and 0 when the field or hash key is absent.
HEXISTS tests whether one field is present in the hash stored at a key. It returns 1 when the field exists and 0 when either the field is absent or the key does not exist; using a non-hash key produces a wrong-type error.
HEXISTS key field| Name | Type | Description |
|---|---|---|
key | key | The name of the key that holds the hash. |
field | field | The field to check for existence. |
Integer reply: 1 if the hash contains the field; 0 if it does not contain the field or the key does not exist.
HSET user:42 name "Ada"
HEXISTS user:42 name
HEXISTS user:42 emailCreates a hash field, then checks two fields; Redis returns 1 for the existing name and 0 for the absent email.
Validate whether an optional attribute has been stored before reading or updating it.
Avoid treating a missing hash field as an existing value in application logic.
Test membership of feature flags, user attributes, or other hash-backed records.
HEXISTS takes a hash key and one field name. It returns 1 when that field exists in the hash, otherwise 0, including when the key is missing.
Follow-up: What is its time complexity? It is O(1), so checking one field does not require scanning the hash.
The command accepts exactly two arguments after the command name: the key containing the hash and the field to test. Its syntax is HEXISTS key field.
Follow-up: How would you check several fields? Issue separate HEXISTS commands or use a pipelined client operation; HEXISTS itself checks one field per invocation.
A result of 0 is ambiguous: the hash may not contain the field, or the key may not exist. A result of 1 means the field is present in the hash.
Follow-up: What happens if the key stores a string, list, or another non-hash type? Redis reports a wrong-type error rather than returning 0 or 1.
HSETHGETHDELHKEYSHLEN