Use SDIFFCARD to count the members of the first set that are not present in any of the other sets. It returns only the count; use SDIFF to retrieve the members.
Keys that do not exist count as empty sets, so a missing first key returns 0. The command does not modify the source sets or store the difference.
LIMIT stops counting once that many members have been found and returns the limit. For example, LIMIT 1 checks whether the difference contains any members. Omitting LIMIT or using LIMIT 0 returns the full count.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<numkeys> | Yes | No | Positive integer specifying how many key arguments follow. |
<key> | Yes | Yes | Set key. Members of all subsequent sets are excluded from the first set. |
LIMIT <limit> | No | No | Non-negative integer that caps the returned count; 0 means no limit. |
Important points#
numkeysmust equal the number of key arguments that immediately follow it.- Key order matters: the command subtracts the other sets from the first set.
- With one key, the command returns that set's cardinality, capped by a positive
LIMIT.
Response#
The number of members in the difference, capped by LIMIT when it is positive.
| Protocol | Reply |
|---|---|
| RESP2 | Integer |
| RESP3 | Integer |
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. Client examples query set1 and set2 from the Redis CLI example.
Redis CLI
The difference contains a and b, so its cardinality is 2.
@upstash/redis
This command is not supported yet in @upstash/redis.
upstash_redis
This command is not supported yet in upstash_redis.