Redis ZINCRBY – How to increment the score of element in sorted set value in redis

3 weeks ago Lalit Bhagtani 0

In this tutorial, we will learn about how to increment the score of an element of the sorted set value stored at a key in redis datastore. For this we will use a COMMAND – ZINCRBY in redis-cli. 

This command is used to increment the score of an element in the sorted set value stored at the key by a specified value (increment). If given element does not exist in the sorted set value, then it is added with the specified value (increment) as its score. If key does not exist, a new sorted set is created with the given element as its only member. The increment value should be the string representation of a numeric value, which includes positive and negative integer numbers and double precision floating point numbers. If negative number is passed as an argument then score will be decremented.  

An error is returned, when key exist but value stored at the key is not of sorted set datatype.

The syntax of redis ZINCRBY command is as follows :-

Syntax :-

redis host:post> ZINCRBY <keyname> <increment> <element>

Output :-

- (string) reply, representing the new score of an element.
- Error, if key exist and value stored at the key is not a sorted set.

Example :-

Redis ZINCRBY

References :-

  1. ZINCRBY Command Docs

That’s all for how to increment the score of an element in the sorted set value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.