0
0
Redisquery~5 mins

HSET and HGET for fields in Redis

Choose your learning style9 modes available
Introduction
HSET and HGET let you store and get small pieces of information inside a bigger group, like putting notes in labeled folders.
You want to save user details like name and age under one user ID.
You need to update or read a specific setting in a configuration without changing others.
You want to keep track of scores for different players in a game.
You want to store product details like price and stock count under one product ID.
Syntax
Redis
HSET key field value
HGET key field
HSET adds or updates a field with a value inside a key that holds many fields.
HGET retrieves the value of a specific field inside the key.
Examples
Stores the name 'Alice' in the 'name' field under the key 'user:1'.
Redis
HSET user:1 name "Alice"
Gets the value stored in the 'name' field under 'user:1', which is 'Alice'.
Redis
HGET user:1 name
Sets the price field to 29.99 for the product with key 'product:100'.
Redis
HSET product:100 price 29.99
Retrieves the price of the product 'product:100'.
Redis
HGET product:100 price
Sample Program
This example stores age and city for user 42, then retrieves both values.
Redis
HSET user:42 age 30
HSET user:42 city "New York"
HGET user:42 age
HGET user:42 city
OutputSuccess
Important Notes
If the field does not exist, HGET returns nil (no value).
HSET creates the key if it does not exist already.
You can update a field by calling HSET again with the same field name.
Summary
HSET stores or updates a field inside a key.
HGET retrieves the value of a specific field.
They help organize related data under one key like a mini-database.