Operations with Numbers
Operations with Numbers
Welcome back! Now that you've learned how to work with numbers in Redis, it's time to build on that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations like incrementing, decrementing, and modifying numeric values directly in Redis.
What You'll Learn
In this lesson, you will learn how to:
- Increment and decrement numeric values.
- Modify numeric values using operations such as increments by a floating point.
Here's the code snippet that we'll be working with:
- After setting initial values for
count,completion_rate, andduration, we perform various operations:decroperation decrements the value ofcountby 1, anddecrBydecrements it by the specified value, in this case, 2. So, the final value ofcountis 2.incroperation increments the value ofdurationby 1, andincrByincrements it by the specified value, in this case, 2. So, the final value ofdurationis 3.incrByFloatincrements the value ofcompletion_rateby the specified floating-point value, in this case, 1.5. So, the final value ofcompletion_rateis 97.
- At the end, we fetch the updated values of
count,duration, andcompletion_rateand log them to the console.
Note, that the incr, decr, incrBy, and decrBy operations cannot be applied to keys that contain floating-point values, that's why we use incrByFloat to increment floating-point values. Note, that in order to decrement floating-point values, you can use incrByFloat with a negative value.
