Concept Flow - HSET and HGET for fields
Start
HSET key field value
Store field-value in hash
HGET key field
Retrieve value for field
Output value
End
The flow shows setting a field-value pair in a hash with HSET, then retrieving the value with HGET.
HSET user:1 name "Alice" HSET user:1 age "30" HGET user:1 name HGET user:1 age
| Step | Command | Action | Result | Hash State |
|---|---|---|---|---|
| 1 | HSET user:1 name "Alice" | Set field 'name' to 'Alice' in 'user:1' | 1 (new field added) | {"name": "Alice"} |
| 2 | HSET user:1 age "30" | Set field 'age' to '30' in 'user:1' | 1 (new field added) | {"name": "Alice", "age": "30"} |
| 3 | HGET user:1 name | Get value of field 'name' from 'user:1' | Alice | {"name": "Alice", "age": "30"} |
| 4 | HGET user:1 age | Get value of field 'age' from 'user:1' | 30 | {"name": "Alice", "age": "30"} |
| 5 | HGET user:1 email | Get value of non-existing field 'email' | nil (field does not exist) | {"name": "Alice", "age": "30"} |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| user:1 hash | {} | {"name": "Alice"} | {"name": "Alice", "age": "30"} | {"name": "Alice", "age": "30"} | {"name": "Alice", "age": "30"} | {"name": "Alice", "age": "30"} |
| name field value | nil | Alice | Alice | Alice | Alice | Alice |
| age field value | nil | nil | 30 | 30 | 30 | 30 |
| email field value | nil | nil | nil | nil | nil | nil |
HSET key field value - sets a field in a hash. Returns 1 if new field added, 0 if updated. HGET key field - gets the value of a field. Returns nil if field does not exist. Used to store and retrieve small sets of key-value pairs inside a Redis key.
HSET do?HSET command is used to store or update a field-value pair inside a Redis hash key, organizing related data together.HGET retrieves values, HDEL deletes fields, and KEYS lists keys, so they do not match the description of HSET.name to John in hash key user:1 using Redis?HSET key field value. Here, key is user:1, field is name, and value is John.HSET user:2 age 30HSET user:2 city LondonHGET user:2 ageage to 30 in user:2. The second sets city to London.HGET user:2 age retrieves the value of field age from user:2, which was set to 30.HGET user:3 email but get (nil) as output. What could be the reason?HGET returns (nil), it means the requested field does not exist in the hash key.(nil). Server down would cause connection errors, and incorrect syntax would cause errors, not (nil).email does not exist in the hash user:3 -> Option Cprofile:100. You run:HSET profile:100 name AliceHSET profile:100 age 0HGET profile:100 age0 is stored as the string "0", which is a valid value.HGET returns the exact stored string. Since age was set to 0, it returns "0", not nil or empty.Pass the test and this pattern moves unknown