Redis SADD – How to create and add elements in the set

2 months ago Lalit Bhagtani 0

In this tutorial, we will learn about how to create and add elements in a set value stored at the key, by using a COMMAND – SADD in redis-cli. If a key exists in the datastore, all the specified elements will be added (ignoring the elements that are already present ) to the set otherwise a new set will be created before adding them to set. The syntax of redis SADD command is as follows :-

Syntax :-

redis host:post> SADD <key name> <value 1> [ <value 2> ]

Output :- 

- (integer value), representing the number of elements added to the set, not including all the elements
  that were already present.
- error, if key exist and value stored at the key is not a set.

Example :-

Redis SADD

References :-

  1. SADD Command Docs

That’s all for how to create and add elements in a set value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.