Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

ZUNIONSTORE

Last updated
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM | MIN | MAX]
Group
Sorted set
Complexity
O(N)+O(M*log(M)) where N is the total size of the input sets and M is the size of the result
Arity
-4 (at least 4 words, counting the command name)
Flags
write
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

ZUNIONSTORE computes the same union as ZUNION and stores it as a sorted set at destination, replacing whatever was there. If the result is empty, destination is deleted. The inputs are read and the result written as one atomic step, and destination can also be one of the inputs.

Inputs can be sorted sets or plain sets (members score 1); missing keys count as empty.

Option Effect
numkeys How many keys follow. Must be at least 1.
WEIGHTS weight ... One multiplier per input key, applied before combining. Default 1 each.
AGGREGATE SUM | MIN | MAX How scores of a shared member combine. Default SUM.

WITHSCORES is not accepted.

Case RESP2 RESP3
Success Integer: the number of members in the stored sorted set Same

Errors are those of ZUNION, with the command named zunionstore in the numkeys error; WITHSCORES gives ERR syntax error.

127.0.0.1:6379> ZADD q1 10 "alice" 5 "bob"
(integer) 2
127.0.0.1:6379> ZADD q2 3 "bob" 7 "carol"
(integer) 2
127.0.0.1:6379> ZUNIONSTORE total 2 q1 q2
(integer) 3
127.0.0.1:6379> ZRANGE total 0 -1 WITHSCORES
1) "carol"
2) "7"
3) "bob"
4) "8"
5) "alice"
6) "10"

Same as Redis 7.4 and Valkey 8.

ZUNION, ZINTERSTORE, ZDIFFSTORE, SUNIONSTORE