w3resource

Redis Sets: SUNIONSTORE

SUNIONSTORE destination key1 [key2]

Redis SUNIONSTORE command is used to store, the members of the set resulting from the union of all the given sets in a specific key. Keys that do not exist are considered to be empty sets and overwritten if specified key already exists.

Syntax:

SUNIONSTORE DESTINATION KEY KEY1..KEYN

Available since:

1.0.0.

Return Value:

Integer replies the number of elements in the resulting set.

Return Value Type:

Integer

Example : Redis SUNIONSTORE : Using two sets

mycolor1 = {R G B} 

mycolor2 = {G B Y}

SUNIONSTORE dest_key mycolor1 mycolor2 = {R G B Y}

127.0.0.1:6379> SADD mycolor1 R G B
(integer) 3
127.0.0.1:6379> SADD mycolor2 G B Y
(integer) 3
127.0.0.1:6379> SUNIONSTORE dest_key mycolor1 mycolor2
(integer) 4
127.0.0.1:6379> SMEMBERS dest_key
1) "G"
2) "B"
3) "Y"
4) "R"

Example : Redis SUNIONSTORE : Using three sets

mycolor1 = {R G B} 

mycolor2 = {G B Y}

mycolor3 = {B O P}

SUNIONSTORE dest_key mycolor1 mycolor2 mycolor3 = {R G B Y O P}

If the destination key already exists, the existing contents will be erased, and written this new set.

127.0.0.1:6379> SADD mycolor3 B O P
(integer) 3
127.0.0.1:6379> SUNIONSTORE dest_key mycolor1 mycolor2 mycolor3
(integer) 6
127.0.0.1:6379> SMEMBERS dest_key
1) "Y"
2) "R"
3) "O"
4) "P"
5) "G"
6) "B"

Example : Redis SUNIONSTORE : destination key can be assigned to a source key

mycolor1 = {R G B} 

mycolor2 = {G B Y}

mycolor3 = {B O P}

SUNIONSTORE mycolor1 mycolor1 mycolor2 mycolor3

the existing contents of mycolor1 are cleared, the results are entered in mycolor1.

127.0.0.1:6379> SUNIONSTORE mycolor1 mycolor1 mycolor2 mycolor3
(integer) 6
127.0.0.1:6379> SMEMBERS mycolor1
1) "Y"
2) "R"
3) "O"
4) "P"
5) "G"
6) "B"


Follow us on Facebook and Twitter for latest update.