HSETNX(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ 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: 0 if the field already exists in the hash and no operation was performed 😭
  • valkey-protocol(7) Integer reply: 1 if 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 πŸ’œ


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