Redis HINCRBY – How to increment a number stored at field in hash value

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to increment the number stored at a field inside the hash value stored at a key in redis datastore. For this we will use a COMMAND – HINCRBY in redis-cli. 

This command is used to increment the number stored at a specified field in the hash value stored at key by a specified value (increment). If given field does not exist in the hash value, then it is added with the specified value increment as its value. If key does not exist, a new hash value is created with the given field as its only member. The increment value is limited to 64 bit signed integers, which means both positive and negative integer can be used. If negative number is passed as an argument then field value will be decremented.  

An error is returned, when key exist but value stored at a key is not of hash datatype.The syntax of redis HINCRBY command is as follows :-

Syntax :- 

redis host:post> HINCRBY <keyname> <field> <increment>

Output :-

- (integer) reply, representing the new value stored at the field.
- Error, if key exist and value stored at the key is not a hash.

Example :-

Redis HINCRBY

References :-

  1. HINCRBY Command Docs

That’s all for how to increment the number stored at a field in the hash value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.