English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Hsetnx Command

Redis Hashes (Hash)

The Redis Hsetnx command is used to assign values to fields that do not exist in the hash table.

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 invalid.

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

Set successfully, return 1 . If the given field already exists and no operation is performed, return 0.

Online Examples

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       # Invalid operation, key-value-store already exists
(integer) 0

Redis Hashes (Hash)