w3resource logo


Redis Tutorial

Redis Sets: SADD

Secondary Nav

SADD key member1 [member2]

Redis SADD command is used to add members to set stored at key. If the member already exists, then it is ignored. If the key does not exist, then a new set is created and members are added into it. If the value stored at key if not set, then an error is returned.

Syntax

SADD KEY_NAME VALUE1..VALUEN

Available since

1.0.0.

Return Value

Integer reply, the number of elements that were added to the set, not including all the elements already present into the set.

Return Value Type

Integer

Example : Redis SADD

127.0.0.1:6379> SADD mycolor "White"
(integer) 1
127.0.0.1:6379> SADD mycolor "Yellow" "Green"
(integer) 2
127.0.0.1:6379> SADD mycolor "Red" "Blue" "Orange"
(integer) 3
127.0.0.1:6379> SMEMBERS mycolor
1) "Yellow"
2) "White"
3) "Blue"
4) "Green"
5) "Red"
6) "Orange"

Example : Redis SADD : Not Allowed Duplicate Member

In the set, member can not be redundant.

127.0.0.1:6379> SADD mycolor "Orange"
(integer) 0
127.0.0.1:6379> SADD mycolor "Orange" "Pink"
(integer) 1
127.0.0.1:6379> SMEMBERS mycolor
1) "Blue"
2) "White"
3) "Yellow"
4) "Pink"
5) "Green"
6) "Red"
7) "Orange"



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program