Redis - Sorted Set Zunionstore Command



Redis ZUNIONSTORE command calculates the union of numkeys sorted sets given by the specified keys, and stores the result in the destination. It is mandatory to provide the number of input keys (numkeys) before passing the input keys and the other (optional) arguments.

Return Value

Integer reply, the number of elements in the resulting sorted set at the destination.

Syntax

Following is the basic syntax of Redis ZUNIONSTORE command.

redis 127.0.0.1:6379> ZUNIONSTORE destination numkeys key [key ...] 
[WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

Example

redis 127.0.0.1:6379> ZADD myzset1 1 b 2 c 
(integer) 2 
redis 127.0.0.1:6379> ZADD myzset2 1 b 2 c 3 d 
(integer) 3 
redis 127.0.0.1:6379> ZUNIONSTORE out 2 myzset1 myzset2 WEIGHTS 2 3 
(integer) 3 
redis 127.0.0.1:6379> ZRANGE out 0 -1 WITHSCORES  
1) "b" 
2) "5" 
3) "c" 
4) "9" 
5) "d" 
6) "10" 
redis_sorted_sets.htm
Advertisements