Model flat records
Store a compact record under one Redis key.
Redis · Command
Creates or updates one or more fields in a Redis hash. It returns how many fields were newly added, excluding fields whose existing values were overwritten.
HSET writes one or more field-value pairs into the hash stored at a key. Existing fields are overwritten; if the key does not exist, Redis creates a new hash. The integer reply counts only fields that were newly added, not fields whose existing values were changed.
HSET key field value [field value ...]| Name | Type | Description |
|---|---|---|
key | key | The Redis key containing the target hash. |
field | field | The first hash field to set. |
value | value | The value assigned to the first field. |
[field value ...] | field-value pairs | Optional additional field-value pairs, repeated as needed. |
An integer equal to the number of fields newly added to the hash; updates to existing fields are not counted.
HSET user:42 name "Ada" role "engineer"
HGETALL user:42Creates or updates two fields, then reads the complete hash.
Store a compact record under one Redis key.
Change selected attributes without replacing the whole hash.
Initialize several related attributes in one command.
HSET stores field-value pairs in a Redis hash and returns the number of fields that were newly created. Updating an existing field returns zero for that field, so the reply is not the total number of pairs written.
Follow-up: How can several fields be written atomically from the command interface? Supply multiple field-value pairs in one HSET invocation; the command accepts repeated pairs after the first one.
If the key is absent, HSET creates a key containing a hash. If the key already contains a hash, the specified fields are added or modified.
Follow-up: What should be checked when the key has another Redis data type? The command cannot operate on that incompatible type and should be treated as a wrong-type error.
The command accepts a key followed by one field and value, with optional additional field-value pairs. Its syntax is HSET key field value [field value ...].
Follow-up: Which command retrieves one field after writing it? HGET reads the value associated with a specified hash field.
HGETHGETALL