The SMEMBERS command retrieves all members from a set stored at a specified key in DiceDB. Sets are unordered collections of unique strings, and this command provides a way to access all elements within a set for inspection or processing.
Syntax
Parameters
Parameter
Description
Type
Required
key
The key identifying the set to retrieve members from
string
Yes
Return values
Condition
Return Value
Key exists and is a set
Array containing all members of the set
Key does not exist
Empty array
Key exists but is not a set
Error message indicating wrong type
Behaviour
Checks if the provided key exists in the database
Verifies if the key is associated with a set data structure
If key exists and is a set, retrieves all members from the set
Returns an empty array if the key doesn’t exist
Returns error if key exists but holds a different data type
The order of elements in the returned array is not guaranteed to be consistent
Errors
Wrong type of key:
Error Message: (error) WRONGTYPE Operation against a key holding the wrong kind of value
Occurs when attempting to use SMEMBERS on a key that contains a non-set value
Example Usage
Basic Usage
Empty Set
Invalid Usage
Wrong Type Error
Best Practices
Always verify the key type before using SMEMBERS to avoid type errors
Implement proper error handling in your application for WRONGTYPE errors
Use EXISTS command to check for key presence before SMEMBERS if key existence is uncertain
Notes
The command returns all members at once, which may impact performance for very large sets
The order of returned elements is not guaranteed and may vary between calls
Memory usage scales linearly with the size of the set
This command has O(N) time complexity where N is the set size