Easy Tutorial
❮ Server Role Redis Sorted Sets ❯

Redis Hsetnx Command

Redis Hashes

The Redis Hsetnx command is used to set the value of a field in a hash table that does not already exist.

If the hash table does not exist, a new hash table is created and the HSET operation is performed.

If the field already exists in the hash table, the operation is ineffective.

If the key does not exist, a new hash table is created and the HSETNX command is executed.

Syntax

The basic syntax for the redis Hsetnx command is as follows:

redis 127.0.0.1:6379> HSETNX KEY_NAME FIELD VALUE

Available Versions

= 2.0.0

Return Value

Returns 1 if the setting is successful. Returns 0 if the given field already exists and no operation is performed.

Example

redis 127.0.0.1:6379> HSETNX myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSETNX myhash field1 "bar"
(integer) 0
redis 127.0.0.1:6379> HGET myhash field1
"foo"

redis 127.0.0.1:6379> HSETNX nosql key-value-store redis
(integer) 1

redis 127.0.0.1:6379> HSETNX nosql key-value-store redis       # Operation is ineffective, key-value-store already exists
(integer) 0

Redis Hashes

❮ Server Role Redis Sorted Sets ❯