In Redis, we can use the SISMEMBER
command to find out whether or not a set contains a given member.
Syntax
The syntax goes like this:
SISMEMBER key member
Example
Suppose we create the following set:
SADD animals Cat Dog Mouse
We can use the SISMEMBER
command to find out whether our set contains a specified value:
SISMEMBER animals Dog
Result:
(integer) 1
In this case we get an integer reply of 1
, which means that the set does contain the value.
Here’s what happens when the set doesn’t contain the value:
SISMEMBER animals Horse
Result:
(integer) 0
Here’s what happens when I pass a value that exists, but with a different case:
SISMEMBER animals dog
Result:
(integer) 0
No match.
Non-Existent Key
Here’s what happens when I pass a key that doesn’t exist:
SISMEMBER oops dog
Result:
(integer) 0
Passing the Wrong Number of Arguments
Passing the wrong number of arguments results in an error:
SISMEMBER animals Dog Mouse
Result:
(error) ERR wrong number of arguments for 'sismember' command