Hashes Hsetnx
# Redis Hsetnx Command
[!(#) Redis Hash](#)
The Redis Hsetnx command is used to assign a value to a field that does not exist in the hash table.
If the hash table does not exist, a new hash table is created and an 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 of the redis Hsetnx command is as follows:
redis 127.0.0.1:6379> HSETNX KEY_NAME FIELD VALUE
### Available Version
>= 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 invalid, key-value-store already exists
(integer) 0
[!(#) Redis Hash](#)
YouTip