Sorted Set

ZADD

Add a member to a sorted set, or update its score if it already exists.

Arguments

keybodystrrequired

The key of the sorted set.

scoresbodyDict[str, float]required

A dictionary of elements and their scores.

xxbodybool

Only update elements that already exist. Never add elements.

nxbodybool

Only add new elements. Never update elements.

gtbodybool

Update scores if the new score is greater than the old score.

ltbodybool

Update scores if the new score is less than the old score.

chbodybool

Return the number of elements changed instead.

incrbodybool

When this option is specified ZADD acts like ZINCRBY. Only one score-element pair can be specified in this mode.

Response

intrequired

The number of elements added to the sorted sets, not including elements already existing for which the score was updated.

If ch was specified, the number of elements that were updated.

If incr was specified, the new score of member.

Simple
# Add three elementsassert redis.zadd("myset", {    "one": 1,    "two": 2,    "three": 3}) == 3# No element is added since "one" and "two" already existassert redis.zadd("myset", {    "one": 1,    "two": 2}, nx=True) == 0# New element is not added since it does not existassert redis.zadd("myset", {    "new-element": 1}, xx=True) == 0# Only "three" is updated since new score was greaterassert redis.zadd("myset", {    "three": 10, "two": 0}, gt=True) == 1# Only "three" is updated since new score was greaterassert redis.zadd("myset", {    "three": 10,    "two": 0}, gt=True) == 1
Loading search…