hset
HSET
Set a field and value
The usage is hset key field value.
You can store multiple fields and values in a key. If the same field already exists, it is overwritten.
The structure is similar to a relational database table. However, fields can differ per key.
Example
| Command> | hset user-1 email charlie@redisgate.com |
| Result> | 1 |
| Command> | hset user-1 language english |
| Result> | 1 |
| Command> | hset user-1 gender m |
| Result> | 1 |
| Command> | hgetall user-1 |
| Result> |
0) email -> charlie@redisgate.com
1) language -> english
2) gender -> m
|
View animation
|
When you run hgetall, redis-cli shows the field and value on separate lines, whereas
Redis Web Client shows them on one line in field -> value form as in the result above.
|
From version 4.0.0 you can specify multiple fields and values.
From version 4.0.0, using hset instead of hmset is recommended.
Example
| Command> | hset myhash f1 v1 f2 v2 |
| Result> | 2 |
Syntax
HSET key field value [field value ...]
- Available since version 2.0.0.
- Time complexity is O(1).