search – not able to insert data using ZADD(sorted set ) in redis using python

search – not able to insert data using ZADD(sorted set ) in redis using python

@TheDude is almost close.

The newer version of redis from (redis-py 3.0), the method signature has changed.
Along with ZADD, MSET and MSETNX signatures were also changed.

The old signature was:

data = hello world
score = 1 
redis.zadd(redis_key_name, data, score) # not used in redis-py > 3.0

The new signature is:

data = hello world
score = 1 

redis.zadd(redis_key_name, {data: score})

To add multiple at once:

data1 = foo
score1 = 10

data2 = bar
score2 = 20

redis.zadd(redis_key_name, {data1: score1, data2: score2})

Instead of args/kwargs, a dict is expected, with key as data and value is the ZADD score.
There are no changes in retrieving the data back.

rediscleint.execute_command(ZADD, rishu, 1, 123).this one works …trying to figure how to add elements to sorted sets without using execute_command approach.

search – not able to insert data using ZADD(sorted set ) in redis using python

@divyanayan awasthi answered:

rediscleint.execute_command(ZADD, rishu, 1, 123)

we can execute raw queries.

Further explanations :

In redis-cli

>>> zadd rishu nx  1 123
# sorted set key = rishu 
# nx = new item 
# score = 1
# member = 123

Now our command in python will be

rediscleint.execute_command(ZADD, rishu,nx 1, 123)

In above code we added new argument into zadd command is nx (add new item).
If we want to update sorted set member then we pass xx instead of nx.

in execute_command we can run same redis command separated by commas.

See also:

Redis sorted set commands

Leave a Reply

Your email address will not be published.