Back to Redis

HSETNX

content/commands/hsetnx.md

latest1.5 KB
Original Source

Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.

Examples

{{% redis-cli %}} HSETNX myhash field "Hello" HSETNX myhash field "World" HGET myhash field {{% /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="hsetnx-return-info" tab1="RESP2" tab2="RESP3" >}}

One of the following:

  • Integer reply: 0 if the field already exists in the hash and no operation was performed.
  • Integer reply: 1 if the field is a new field in the hash and the value was set.

-tab-sep-

One of the following:

  • Integer reply: 0 if the field already exists in the hash and no operation was performed.
  • Integer reply: 1 if the field is a new field in the hash and the value was set.

{{< /multitabs >}}