Redis - Hash Hsetnx Command


Advertisements

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

Return Value

Integer reply

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

Syntax

Following is the basic syntax of Redis HSETNX command.

redis 127.0.0.1:6379> HSETNX KEY_NAME FIELD VALUE

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_hashes.htm
Advertisements