0
0
Redisquery~10 mins

HEXISTS for field check in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HEXISTS for field check
Start
Call HEXISTS key field
Check if key exists?
NoReturn 0 (field not found)
Yes
Check if field exists in hash
Return 1 (field exists)
Return 0 (field not found)
HEXISTS checks if a specific field exists inside a hash stored at a key. It returns 1 if found, otherwise 0.
Execution Sample
Redis
HSET user:1 name "Alice"
HSET user:1 age 30
HEXISTS user:1 name
HEXISTS user:1 email
Set fields 'name' and 'age' in hash 'user:1', then check if 'name' and 'email' fields exist.
Execution Table
StepCommandKey Exists?Field Exists?Return Value
1HSET user:1 name "Alice"No -> Yes (created)name added1 (field added)
2HSET user:1 age 30Yesage added1 (field added)
3HEXISTS user:1 nameYesYes1
4HEXISTS user:1 emailYesNo0
💡 HEXISTS returns 1 if field exists in hash, else 0; key must exist for field check
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
user:1 hash fieldsempty{"name": "Alice"}{"name": "Alice", "age": "30"}{"name": "Alice", "age": "30"}{"name": "Alice", "age": "30"}
Key Moments - 2 Insights
Why does HEXISTS return 0 for 'email' even though the key 'user:1' exists?
HEXISTS checks if the specific field exists inside the hash. In step 4, 'email' field is not present in 'user:1', so it returns 0 (see execution_table row 4).
What happens if the key does not exist at all when HEXISTS is called?
If the key does not exist, HEXISTS immediately returns 0 because there is no hash to check fields in (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does HEXISTS return at step 3 for field 'name'?
A1
Bnull
C0
Derror
💡 Hint
Check the 'Return Value' column in execution_table row 3
At which step does the key 'user:1' get created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at 'Key Exists?' column in execution_table row 1
If we check HEXISTS for a field on a non-existent key, what would be the return value?
A1
B0
Cnull
Derror
💡 Hint
Refer to concept_flow where key existence is checked first
Concept Snapshot
HEXISTS key field
- Checks if 'field' exists in hash at 'key'
- Returns 1 if field exists, 0 otherwise
- Returns 0 if key does not exist
- Useful to test presence of a field before reading or updating
Full Transcript
HEXISTS is a Redis command to check if a specific field exists inside a hash stored at a given key. When you run HEXISTS with a key and a field, Redis first checks if the key exists. If the key does not exist, it returns 0 immediately. If the key exists, it then checks if the field exists inside the hash. If the field is found, it returns 1; if not, it returns 0. For example, if you set fields 'name' and 'age' in a hash called 'user:1', then HEXISTS user:1 name returns 1 because 'name' exists, but HEXISTS user:1 email returns 0 because 'email' was never set. This command helps you know if a field is present before trying to read or modify it.