Back to Redis

HSET

content/commands/hset.md

latest1.8 KB
Original Source

Sets the specified fields to their respective values in the hash stored at key.

This command overwrites the values of specified fields that exist in the hash. If key doesn't exist, a new key holding a hash is created.

Examples

{{< clients-example set="cmds_hash" step="hset" description="Foundational: Set one or more field-value pairs in a hash using HSET (creates hash if needed, overwrites existing fields, returns count of new fields)" difficulty="beginner" >}}

HSET myhash field1 "Hello" (integer) 1 HGET myhash field1 "Hello" HSET myhash field2 "Hi" field3 "World" (integer) 2 HGET myhash field2 "Hi" HGET myhash field3 "World" HGETALL myhash

  1. "field1"
  2. "Hello"
  3. "field2"
  4. "Hi"
  5. "field3"
  6. "World" {{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}} HSET myhash field1 "Hello" HGET myhash field1 HSET myhash field2 "Hi" field3 "World" HGET myhash field2 HGET myhash field3 HGETALL myhash {{% /redis-cli %}}

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |

Return information

{{< multitabs id="hset-return-info" tab1="RESP2" tab2="RESP3" >}}

Integer reply: the number of fields that were added.

-tab-sep-

Integer reply: the number of fields that were added.

{{< /multitabs >}}