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.


Following is the basic syntax of Redis ZUNIONSTORE command.

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


redis> ZADD myzset1 1 b 2 c 
(integer) 2 
redis> ZADD myzset2 1 b 2 c 3 d 
(integer) 3 
redis> ZUNIONSTORE out 2 myzset1 myzset2 WEIGHTS 2 3 
(integer) 3 
redis> ZRANGE out 0 -1 WITHSCORES  
1) "b" 
2) "5" 
3) "c" 
4) "9" 
5) "d" 
6) "10"