How do I get the size of a set on Redis?

If it's a sorted set, you can use

ZCOUNT myset -inf +inf

or

ZCARD myset

You are looking for the SCARD command:

SCARD key

Returns the set cardinality (number of elements) of the set stored at

Return value
Integer reply: the cardinality (number of elements) of the set, or 0 if key does not exist.

You can view all of the set commands on the documentation webpage.


  • zCard is short for cardinality (cardinality is the number of elements in a set). It gives you total number of members inside of a "sorted set".

  • Sometimes you might wanna extract how many members are inside of a range in a sorted set. For that you can use zCount.

    ZCOUNT cars 0 50  // inclusive
    

this will include 0 and 55. 0 <= .... <=50. But if you do not want to include them

ZCOUNT cars (0 (50
  • if it is regular set

    SCARD cars 
    

Tags:

Set

Redis