PASSWORD RESET

Your destination for complete Tech news

What is the difference between HSET and HMSET in Redis

1.96K 0
< 1 min read

In Redis, both the “HSET” and “HMSET” commands are used to set values in a hash, but they have some differences in how they are used.

  • The “HSET” command is used to set a single field-value pair in a hash. It takes three arguments: the key of the hash, the field name, and the value. For example:
HSET myhash field1 value1

This command will set the value “value1” for the field “field1” in the hash with key “myhash”.

  • The “HMSET” command is used to set multiple field-value pairs in a hash at once. It takes a variable number of arguments, where the first argument is the key of the hash, and the following arguments are field-value pairs. For example:
HMSET myhash field1 value1 field2 value2 field3 value3

This command will set the values “value1”, “value2”, and “value3” for the fields “field1”, “field2”, and “field3” in the hash with key “myhash” respectively.

So, in summary, if you need to set just one field-value pair in a hash, you should use the “HSET” command, if you need to set multiple field-value pairs in a hash, you should use the “HMSET” command.

It’s also worth noting that “HSET” returns an integer indicating the number of fields that were added to the hash, because of this, if the field already exists, it just updates its value, and it returns 0. While “HMSET” returns “OK” regardless of whether the field already exists or not.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.