The Redis HINCRBYFLOAT
command increments the field in the hash at the specified key by a floating point number. We specify how much we want the field to increment by when we call the command.
Syntax
The syntax goes like this:
HINCRBYFLOAT key field increment
Example
Here’s an example to demonstrate:
HINCRBYFLOAT player points 2.57
Result:
"2.57"
The command returns a bulk string reply with the value of the field after the operation has taken place.
If the key doesn’t exist, it’s created with the field set to 0
, and then incremented by the specified amount. In my case, the key didn’t exist, and so it was created and incremented as specified.
Non-Existent Fields
If the key exists, but the field doesn’t, the field is created, set to 0
, and then incremented by the specified amount:
HINCRBYFLOAT player credit 51.50
Result:
"51.5"
We can use a command such as HGETALL
to check the key’s fields and their respective values:
HGETALL player
Result:
1) "points" 2) "1.34" 3) "credit" 4) "51.5"
Negative Values
We can also use a negative value to decrement the field:
HINCRBYFLOAT player points -1.23
Result:
"1.34"
Passing the Wrong Number of Arguments
Passing the wrong number of arguments results in an error:
HINCRBYFLOAT player points 10 credit 5
Result:
(error) ERR wrong number of arguments for 'hincrbyfloat' command