w3resource

Redis Hash: HINCRBY

Redis HINCRBY Command

Redis HINCRBY command is used to increment the number stored at the field in the hash stored at key by increment. If the key does not exist, a new key holding a hash is created. If the field does not exist the value is set to 0 before the operation is performed.

Here are some common uses and scenarios for the "HINCRBY" command:

  • Increment Counters: Update counters stored in a hash for tracking purposes.
  • Inventory Management: Adjust stock levels or quantities within a hash.
  • Score Keeping: Increment scores or points for users or items.
  • Atomic Operations: Ensure atomicity when incrementing hash fields in a multi-client environment.
  • Dynamic Data Updates: Efficiently update numeric values within a hash without retrieving and modifying the entire hash.

Syntax:

HINCRBY KEY_NAME FIELD_NAME INCR_BY_NUMBER 

Available since

2.0.0.

Return Value

Integer reply, the value at the field after the increment operation.

Return Value Type

Integer

Example: Redis HINCRBY

127.0.0.1:6379> HSET langhash lang1 10
(integer) 1
127.0.0.1:6379> HINCRBY langhash lang1 1
(integer) 11
127.0.0.1:6379> HINCRBY langhash lang1 -1
(integer) 10

Example: Redis HINCRBY another example

127.0.0.1:6379> HSET user visits 5
(integer) 1
127.0.0.1:6379> HINCRBY user visits 15
(integer) 20
127.0.0.1:6379> HINCRBY user visits -25
(integer) -5

Previous: HGETALL
Next: HINCRBYFLOAT



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-hincrby-key-field-increment.php