Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - HEXISTS for field check
Start
↓
Call HEXISTS key field
↓
Check if key exists?
No→Return 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.

Practice

(1/5)
1. What does the Redis command HEXISTS key field do?
easy
A. Deletes the specified field from the hash
B. Sets a new field in the hash
C. Returns all fields and values in the hash
D. Checks if the specified field exists in the hash stored at key

Solution

  1. Step 1: Understand the HEXISTS command purpose

    HEXISTS checks if a specific field exists inside a Redis hash stored at a given key.
  2. Step 2: Identify the correct behavior

    It returns 1 if the field exists, otherwise 0. It does not delete, list, or set fields.
  3. Final Answer:

    Checks if the specified field exists in the hash stored at key -> Option D
  4. Quick Check:

    HEXISTS checks field existence = B [OK]
Hint: HEXISTS returns 1 if field exists, else 0 [OK]
Common Mistakes:
  • Confusing HEXISTS with HDEL (delete)
  • Thinking HEXISTS returns the field value
  • Assuming HEXISTS lists all fields
2. Which of the following is the correct syntax to check if field age exists in hash user:1 using HEXISTS?
easy
A. HEXISTS user:1 age
B. HEXISTS age user:1
C. HEXISTS user:1:age
D. HEXISTS user:1, age

Solution

  1. Step 1: Recall HEXISTS command syntax

    The correct syntax is HEXISTS key field, where key is the hash key and field is the field name.
  2. Step 2: Match syntax with options

    HEXISTS user:1 age matches the correct syntax: HEXISTS user:1 age. Other options have wrong order or invalid format.
  3. Final Answer:

    HEXISTS user:1 age -> Option A
  4. Quick Check:

    HEXISTS key field syntax = C [OK]
Hint: Remember HEXISTS key field order exactly [OK]
Common Mistakes:
  • Swapping key and field positions
  • Using colon between key and field
  • Adding commas between arguments
3. Given the Redis hash user:2 with fields {"name": "Alice", "email": "alice@example.com"}, what will HEXISTS user:2 phone return?
medium
A. 1
B. Error
C. 0
D. nil

Solution

  1. Step 1: Check if field 'phone' exists in hash user:2

    The hash has fields 'name' and 'email' only. 'phone' is not present.
  2. Step 2: Understand HEXISTS return value

    HEXISTS returns 1 if field exists, 0 if it does not. Since 'phone' is missing, it returns 0.
  3. Final Answer:

    0 -> Option C
  4. Quick Check:

    Field missing returns 0 = D [OK]
Hint: HEXISTS returns 0 for missing fields [OK]
Common Mistakes:
  • Expecting nil instead of 0
  • Thinking it returns an error for missing fields
  • Confusing with HGET which returns nil
4. You run the command HEXISTS user:3 name but get an error. Which of the following is the most likely cause?
medium
A. The field name is misspelled
B. The key user:3 exists but is not a hash
C. HEXISTS requires two fields to check
D. Redis server is down

Solution

  1. Step 1: Understand HEXISTS error causes

    HEXISTS returns error if the key exists but is not a hash type. If key does not exist, it returns 0, no error.
  2. Step 2: Eliminate distractors

    Misspelled field returns 0, no error. HEXISTS checks one field. Server down causes connection failure, not command error. Wrong key type causes WRONGTYPE error.
  3. Final Answer:

    The key user:3 exists but is not a hash -> Option B
  4. Quick Check:

    Wrong key type causes HEXISTS error = A [OK]
Hint: Check key type before HEXISTS to avoid errors [OK]
Common Mistakes:
  • Assuming misspelled field causes error
  • Thinking HEXISTS needs multiple fields
  • Ignoring key type mismatch
5. You want to add a field status to a Redis hash order:100 only if it does not already exist. Which sequence of commands correctly uses HEXISTS to check before adding?
hard
A. HEXISTS order:100 status IF 0 THEN HSET order:100 status 'pending' END
B. HSET order:100 status 'pending' HEXISTS order:100 status
C. HEXISTS order:100 status IF 1 THEN HSET order:100 status 'pending' END
D. HGET order:100 status IF nil THEN HSET order:100 status 'pending' END

Solution

  1. Step 1: Understand the goal

    You want to add 'status' only if it does not exist, so first check with HEXISTS if field is missing (returns 0).
  2. Step 2: Analyze command sequences

    HEXISTS order:100 status IF 0 THEN HSET order:100 status 'pending' END checks HEXISTS, then if result is 0 (field missing), it sets the field. This matches the requirement. HSET order:100 status 'pending' HEXISTS order:100 status sets field unconditionally first. HEXISTS order:100 status IF 1 THEN HSET order:100 status 'pending' END sets field if HEXISTS returns 1 (field exists), which is wrong. HGET order:100 status IF nil THEN HSET order:100 status 'pending' END uses HGET and nil check, which is valid but not using HEXISTS as asked.
  3. Final Answer:

    HEXISTS order:100 status IF 0 THEN HSET order:100 status 'pending' END -> Option A
  4. Quick Check:

    Check HEXISTS=0 before HSET = A [OK]
Hint: Add field only if HEXISTS returns 0 [OK]
Common Mistakes:
  • Setting field without checking existence
  • Checking HEXISTS=1 to add field
  • Using HGET instead of HEXISTS when asked
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard