HSETNX(3valkey)
β¨ HSETNX π
okay so bestie, welcome to the hsetnx page from the valkey command manual β yes, the whole thing, i got you π«Ά
πΈ NAME πΈ
hsetnx β sets the value of a field in a hash only when the field doesn't exist. that's it. that's the whole vibe. one job, and she does it flawlessly π
β¨ SYNOPSIS β¨
HSETNX key field value
π DESCRIPTION π
so here's the tea π β hsetnx sets field inside the hash stored at key to value, but only if field doesn't already exist. no cap.
and if key itself doesn't exist yet? she doesn't ghost you β a whole new key holding a hash gets created, iconic behavior fr fr β¨
but if field is already there?? this operation has no effect. like, nothing happens. she simply says no π
π REPLY π
you get one of these, bestie:
- valkey-protocol(7) Integer reply:
0if the field already exists in the hash and no operation was performed π - valkey-protocol(7) Integer reply:
1if the field is a new field in the hash and the value was set π₯
β COMPLEXITY β
O(1) β constant time, she's fast, periodt π
π ACL CATEGORIES π
@fast @hash @write
π¦ HISTORY π¦
- Available since:
2.0.0β an OG, lowkey legendary π
π EXAMPLES π
127.0.0.1:6379> HSETNX myhash field "Hello"
(integer) 1
127.0.0.1:6379> HSETNX myhash field "World"
(integer) 0
127.0.0.1:6379> HGET myhash field
"Hello"
see how the second call got a 0? the field was already there so "World" never made it in, and HGET still says "Hello". the way she stayed loyal to that first valueβ¦ main character energy π
π SEE ALSO π
- hdel(3valkey)
- hexists(3valkey)
- hexpire(3valkey)
- hexpireat(3valkey)
- hexpiretime(3valkey)
- hget(3valkey)
- hgetall(3valkey)
- hgetex(3valkey)
- hincrby(3valkey)
- hincrbyfloat(3valkey)
- hkeys(3valkey)
- hlen(3valkey)
- hmget(3valkey)
- hmset(3valkey)
- hpersist(3valkey)
- hpexpire(3valkey)
- hpexpireat(3valkey)
- hpexpiretime(3valkey)
- hpttl(3valkey)
- hrandfield(3valkey)
- hscan(3valkey)
- hset(3valkey)
- hsetex(3valkey)
- hstrlen(3valkey)
- httl(3valkey)
- hvals(3valkey)
and that's the whole page bestie, you ate it π this lil guide is from valkey version 9.0.0, dated 2025-10-21 β go forth and set those fields conditionally, i'm obsessed π§πΈπ«Ά
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms